无法从。net应用程序发送post请求,使用Chrome RESTClient可以正常工作

时间:2022-03-14 18:17:51

Why I am not able to send POST request using the following command and some other code similar to it. Where as when I send same request using Mozilla browser's RESTClient it works fine. This is only for a server's simulator deployed on LAN/even on my local machine. with Live server it is working fine.

为什么我不能使用以下命令和其他类似的代码发送POST请求。当我使用Mozilla浏览器的RESTClient发送相同的请求时,它工作得很好。这只适用于部署在LAN上的服务器模拟器,甚至是本地机器上的模拟器。使用Live server,运行良好。

ASCIIEncoding encoding = new ASCIIEncoding();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.220.12:5000"); // this is my system's local ip
byte[] byteArray = encoding.GetBytes("hello");
request.Method = "POST";
request.ContentType = "text/xml";// i tried it with "application/x-www-form-urlencoded" as well
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Flush();
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
Console.ReadKey();
reader.Close();
dataStream.Close();
response.Close();

By testing it on server, I saw there is a message for successful receiving of the request but it does not show the data received from the above code whereas it shows the data from RESTclient.

通过在服务器上测试它,我看到有一条消息可以成功地接收请求,但是它没有显示从上面的代码接收到的数据,而它显示了来自RESTclient的数据。

What could be the possible reason. Is there any firewall the is not allowing to send postdata from .net library to simulator?

可能的原因是什么?是否有防火墙不允许从。net库发送邮件到模拟器?

This is the same response for un formatted xml, I don't see anything wrong with the xml (same copy of what I am sending through RESTclient).

这是对un格式xml的相同响应,我认为xml没有任何问题(与我通过RESTclient发送的内容相同)。

1 个解决方案

#1


3  

It is not clear what content type does the server expects the request to be. If it is application/x-www-form-urlencoded (key=value pairs) you could simplify your code:

不清楚服务器期望请求是什么内容类型。如果是application/x-www-form- urlencodes (key=value pair),可以简化代码:

using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "key1", "value1" },
        { "key2", "value2" },
    };
    byte[] result = client.UploadValues("http://192.168.220.12:5000", values);
    string resultStr = Encoding.Default.GetString(result);
    ...
}

If you want to POST XML then:

如果您想发布XML,那么:

using (var client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "text/xml";
    string xml = "<foo>Bar</foo>";
    string result = client.UploadString("http://192.168.220.12:5000", xml);
}

So as you can see, it will all depend on what does your server side script expects.

正如您所看到的,这一切都取决于服务器端脚本的期望。

#1


3  

It is not clear what content type does the server expects the request to be. If it is application/x-www-form-urlencoded (key=value pairs) you could simplify your code:

不清楚服务器期望请求是什么内容类型。如果是application/x-www-form- urlencodes (key=value pair),可以简化代码:

using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "key1", "value1" },
        { "key2", "value2" },
    };
    byte[] result = client.UploadValues("http://192.168.220.12:5000", values);
    string resultStr = Encoding.Default.GetString(result);
    ...
}

If you want to POST XML then:

如果您想发布XML,那么:

using (var client = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "text/xml";
    string xml = "<foo>Bar</foo>";
    string result = client.UploadString("http://192.168.220.12:5000", xml);
}

So as you can see, it will all depend on what does your server side script expects.

正如您所看到的,这一切都取决于服务器端脚本的期望。