简体   繁体   中英

Time out Exception when invoking GetResponse()

I create a WebRequest to post some HTML content to another web server. When I use normal text content, it works, but when I post HTML content I get a time out error when invoking GetResponse() .

WebResponse response = request.GetResponse()

How can I figure out the issue? I tried adding an error handler for WebException but I could not catch the exception.

Request code :

byte[] byteArray = Encoding.UTF8.GetBytes(sPostData);

// Create a request using a URL that can receive a post. 
WebRequest request = WebRequest.Create(httpUri);
// Set the 'Timeout' property in Milliseconds.
//request.Timeout = 20000;
// Set the ContentType property of the WebRequest.
request.ContentType = contentType;
// Set the Method property of the request to POST.
request.Method = postMethod;
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
using (Stream PostStream = request.GetRequestStream())
{
    PostStream.Write(byteArray, 0, byteArray.Length);
}
// Get the response.
using (WebResponse response = request.GetResponse())
{
    // Get the stream containing content returned by the server.
    using (Stream responseStream = response.GetResponseStream())
    {
        using (StreamReader reader = new StreamReader(responseStream))
        {
            string responseFromServer = reader.ReadToEnd();

            result = responseFromServer;
        }
    }
}

You should look at the network traffic using Fiddler or another equivalent application. This may show you whether the server is responding at all, or perhaps it is responding late.

You should also look at the server logs or the Windows Event Log - perhaps the server doesn't like being sent HTML, and is logging an error. In this case, it may simply decide that you are trying to hack it, and it may decide not to answer you at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM