Timeout on HttpWebRequest.GetRequestStream()

G

Gilberto Araya

I have the following code that invokes a web service:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(wftUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

// Prepare the request stream
StringBuilder requestContent = new StringBuilder("WFTXMLFEED=");
requestContent.Append(HttpUtility.UrlEncode(document.OuterXml));
byte[] encodedRequest = Encoding.UTF8.GetBytes(requestContent.ToString());
request.ContentLength = encodedRequest.Length;

using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(encodedRequest, 0, encodedRequest.Length);
requestStream.Close();
}



The previous code works fine most of the time, but every once in a while a
WebException indicating a timeout is thrown in the GetRequestStream() call.
A similar application using XMLHTTP (COM interfaces) never causes a timeout,
so I doubt it is a problem on the web service side...

Anyone who has an explanation for why this might be happening, or more
importantly, a solution for this problem, I would appreciate it if it is
posted here...

Thanks,

Gilberto Araya
 
J

Joerg Jooss

"Gilberto Araya" spoke:
I have the following code that invokes a web service:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(wftUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

// Prepare the request stream
StringBuilder requestContent = new StringBuilder("WFTXMLFEED=");
requestContent.Append(HttpUtility.UrlEncode(document.OuterXml));
byte[] encodedRequest =
Encoding.UTF8.GetBytes(requestContent.ToString());
request.ContentLength = encodedRequest.Length;

using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(encodedRequest, 0, encodedRequest.Length);
requestStream.Close();
}



The previous code works fine most of the time, but every once in a
while a WebException indicating a timeout is thrown in the
GetRequestStream() call. A similar application using XMLHTTP (COM
interfaces) never causes a timeout, so I doubt it is a problem on
the web service side...

Anyone who has an explanation for why this might be happening, or
more importantly, a solution for this problem, I would appreciate it
if it is posted here...

Maybe HttpWebRequest and XMLTHTTP use different default timeouts. You
can set HttpWebRequest.Timeout to Timeout.Infinite and check if that
solves the problem.

Cheers,
 
G

Gilberto Araya

It's not a problem with the timeout interval. I've set ridiculously high
timeouts to my example, and result is the same (just takes longer for the
exception to be thrown).


Joerg Jooss said:
"Gilberto Araya" spoke:
I have the following code that invokes a web service:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(wftUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

// Prepare the request stream
StringBuilder requestContent = new StringBuilder("WFTXMLFEED=");
requestContent.Append(HttpUtility.UrlEncode(document.OuterXml));
byte[] encodedRequest =
Encoding.UTF8.GetBytes(requestContent.ToString());
request.ContentLength = encodedRequest.Length;

using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(encodedRequest, 0, encodedRequest.Length);
requestStream.Close();
}



The previous code works fine most of the time, but every once in a
while a WebException indicating a timeout is thrown in the
GetRequestStream() call. A similar application using XMLHTTP (COM
interfaces) never causes a timeout, so I doubt it is a problem on
the web service side...

Anyone who has an explanation for why this might be happening, or
more importantly, a solution for this problem, I would appreciate it
if it is posted here...

Maybe HttpWebRequest and XMLTHTTP use different default timeouts. You
can set HttpWebRequest.Timeout to Timeout.Infinite and check if that
solves the problem.

Cheers,
 
T

Tian Min Huang

Hi Gilberto,

There is a known issue where the timeout setting does not take for this
part of the request. The workaround is to override the GetWebRequest
method as follows:

public class Service1Proxy : SoapHttpClientProtocol
{
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest request = base.GetWebRequest(uri);
request.Timeout = this.Timeout;
}
}

I look forward to your result.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top