服务器 returns "Unable to connect to the remote server" 当查询字符串参数在正文中作为字节发送时

Server returns "Unable to connect to the remote server" when querystring parameters sent in body as byte

我有以下代码来生成 POST 并检索响应:

string url = // Url. Example= "https://www.mywebsite.com"
string body = // querystring values I would like to pass as parameter. Example= "Param1=Value1&Param2=Value2&Param3=Value3"

byte[] byteBody = Encoding.GetEncoding("iso-8859-1").GetBytes(body);
httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = false;
httpWebRequest.ContentLength = byteBody.Length;

Stream requestStream = httpWebRequest.GetRequestStream();
requestStream.Write(byteBody, 0, byteBody.Length);
requestStream.Flush();
(HttpWebResponse)httpWebRequest.GetResponse();

当我如下设置 url 和 body 参数时,此代码适用于这么多 URL 没有问题。

string url = "https://www.mywebsite.com";
string body = "Param1=Value1&Param2=Value2&Param3=Value3";

但是有一个 URL 没有。当我像上面那样设置参数时,它 returns "Unable to connect to the remote server".

但是,当我设置如下参数时,它起作用了:

string url = "https://www.mywebsite.com?Param1=Value1&Param2=Value2&Param3=Value3";
string body = "";

会是什么原因?发送 URL 的 Querystring 中的参数和写入流有什么区别? 第一种方法是安全还是更可靠,或者它们之间完全没有区别?

试着把“?” url 字符串中的字符,就在参数字符串之前。

string url = "https://www.mywebsite.com?Param1=Value1&Param2=Value2&Param3=Value3";

好吧,这取决于服务器端对此 api 的期望。 api 正确响应调用

string url = "https://www.mywebsite.com?Param1=Value1&Param2=Value2&Param3=Value3";
string body = "";

表示可能期待 GET 调用而不是 POST。