GetResponse in C # does not work. No response is returned

I have this code in C #:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 30000; 
request.Method = "POST"; 
request.KeepAlive = true;
request.AllowAutoRedirect = false;

Stream newStream = request.GetRequestStream();
newStream.Write(bPostData, 0, bPostData.Length);

byte[] buf = new byte[1025]; int read = 0; string sResp = "";
HttpWebResponse wResp = (HttpWebResponse)request.GetResponse();
Stream resp = wResp.GetResponseStream();

The line HttpWebResponse wResp =...just hangs (as in the answer from the url). I am not sure where exactly its failure is (because I don’t even get an exception error). I tested the url in IE and it works great. I also checked bPostData and I have data. Where is this going?

+3
source share
5 answers

Try closing the request stream in the newStream variable. Perhaps the API is waiting for it to be done.

+3
source

You need to increase the limit:

ServicePointManager.DefaultConnectionLimit = 10; // Max number of requests
+1
source

. , / /. , application/x-www-form-urlencoded HTTP POST . WebClient:

using (var client = new WebClient())
{
    client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
    var values = new NameValueCollection
    {
        { "param1", "value1" },
        { "param2", "value2" },
    };
    byte[] result = client.UploadValues(url, values);
}
0

When I commented earlier, I ran your code in my office (heavily firewall). I got the same result as you. I came home, tried again (less than a firewall), it worked fine ... I assume that you have a barrier. I believe that you are faced with a firewall problem.

0
source

Use content length = 0

Example:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
        request.Method = "POST";
        request.ContentLength = 0;
        var requestStream = request.GetRequestStream();
        HttpWebResponse res = (HttpWebResponse)request.GetResponse();
        res.Close();
0
source

All Articles