1

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;
        }
    }
}
vml19
  • 3,816
  • 11
  • 45
  • 63

1 Answers1

0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Thanks John for your reply. if I modify html as follows it works,

    Test

    I have some Thai characters in HTML, will it be a problem?
    – vml19 Apr 20 '11 at 02:11
  • I use the following, byte[] byteArray = Encoding.UTF8.GetBytes(sPostData); – vml19 Apr 20 '11 at 07:03
  • I checked the event log there is nothing captured when I get this exception. – vml19 Apr 26 '11 at 04:34
  • @Nilaa: I would have to see the code on the server that receives the message. Also, if the server is running ASP.NET, you should try turning on [ASP.NET Health Monitoring](http://msdn.microsoft.com/en-us/library/bb398933.aspx). – John Saunders Apr 26 '11 at 14:34