C# .NET - Time out Exception when invoking GetResponse()

Asked By Vimal Thavraj on 20-Apr-11 01:59 AM

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;
       
}
   
}
}

Jatin Trikha replied to Vimal Thavraj on 20-Apr-11 02:07 AM
following request

--> validate if the url is correct in which you are posting
--> since it a post request validate the data you are posting if its correct
--> if above two steps are positive try access the site in a browser with same data and validate if server is not actually giving server time out
--> Step 3 also passes give increase the timeout for the web request

Also you can use some sniffer to match the request you custom web client and browser is making.

Vimal Thavraj replied to Jatin Trikha on 20-Apr-11 02:26 AM
See my comments to your reply

--> validate if the url is correct in which you are posting
- Correct
--> since it a post request validate the data you are posting if its correct
- Correct
--> if above two steps are positive try access the site in a browser with same data and validate if server is not actually giving server time out
- when I try the same data(HTML Content) with browser form post, it works properly
--> Step 3 also passes give increase the timeout for the web request
- I do not set time out.

I suspect that problem with HTML data as it works fine for plain text data, if so then any suggestion on how to sort this out.
Anoop S replied to Vimal Thavraj on 20-Apr-11 02:28 AM
try by disabling the eventvalidation,now  without any restriction, put javascript or something else in the textbox and this code will be executed. The Server.HTMLEncode will prevent this: o.RichDescription = Server.HTMLEncode(this.TextboxDesc.Text);

add this to your page

<%@ Page Language="C#" MasterPageFile="~/site.master" AutoEventWireup="true"  EnableEventValidation="false" %>
Vimal Thavraj replied to Anoop S on 20-Apr-11 02:31 AM
Actually I am not using asp.net pages here it is a windows application from which I try to post some Email template to another website.
Jitendra Faye replied to Vimal Thavraj on 20-Apr-11 02:49 AM
Follow this link, i have seen same post,

http://boardreader.com/thread/HttpWebRequest_GetResponse_Timeout_excep_1kgX2dnj.html
i hope this will help you.
Jitendra Faye replied to Vimal Thavraj on 20-Apr-11 02:49 AM
Follow this link, i have seen same post,

http://boardreader.com/thread/HttpWebRequest_GetResponse_Timeout_excep_1kgX2dnj.html
i hope this will help you.
Jatin Trikha replied to Vimal Thavraj on 20-Apr-11 03:01 AM
if its not working for HTML content

check the value that you are giving content type
 use switch change the values of request.ContentType...

Vimal Thavraj replied to Jatin Trikha on 20-Apr-11 03:06 AM
I use 'application/x-www-form-urlencoded' for content type for all. is it a correct content type to use with HTML content?
Anoop S replied to Vimal Thavraj on 20-Apr-11 03:07 AM
Event validation reduces the risk of unauthorized postback requests and callbacks, so in website most commonly(by default)  the event validation set to true, so you need to edit in website. otherwise i don't think you can send html or javascript
Vimal Thavraj replied to Anoop S on 20-Apr-11 03:22 AM
If my HTML like this,

<html><body><p>Hello world.</p></body></html> it works properly.

If I use like this, then the problem

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table id="Table_01" width="600" height="1201" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="5"><a href="http://www.dumex.co.th/immunity/index.htm?utm_source=Direct_email&utm_medium=Email&utm_campaign=Immunity_challenge" target="_blank"><img src="http://prelive.publication.tra.co.th/Thailand/images/images/enews-3sea-2_01.jpg" alt="" width="600" height="358" border="0" /></a></td>
</tr>
<tr>
<td colspan="5"><a href="http://www.dumex.co.th/immunity/index.htm?utm_source=Direct_email&utm_medium=Email&utm_campaign=Immunity_challenge" target="_blank"><img src="http://prelive.publication.tra.co.th/Thailand/images/images/enews-3sea-2_02.jpg" alt="" width="600" height="419" border="0" /></a></td>
</tr>
<tr>
<td colspan="5"><a href="http://www.dumex.co.th/immunity/index.htm?utm_source=Direct_email&utm_medium=Email&utm_campaign=Immunity_challenge" target="_blank"><img src="http://prelive.publication.tra.co.th/Thailand/images/images/enews-3sea-2_03.jpg" alt="" width="600" height="369" border="0" /></a></td>
</tr>
<tr>
<td colspan="2">
<img src="http://prelive.publication.tra.co.th/Thailand/images/images/enews-3sea-2_07.jpg" width="275" height="37" alt=""/></td>
<td colspan="2"><a href="http://www.dumex.co.th/my_dumex/my_subscriptions" target="_blank"><img src="http://prelive.publication.tra.co.th/Thailand/images/images/enews-3sea-2_08.jpg" alt="" width="89" height="37" border="0" /></a></td>
<td>
<img src="http://prelive.publication.tra.co.th/Thailand/images/images/enews-3sea-2_09.jpg" width="236" height="37" alt=""/></td>
</tr>
</table>

</body>
</html>