System.Net.WebException: The operation has timed out

M

Matt H.

I am having a problem with a HTTPWebrequest that I am posting to a
third party server. This is an ecommerce scenario where someone is
retrieving their bill information from our server and then we are
passing them off to a third party to collect the payment.

On our server they are in an HTTPS state, and we are passing them off
to another HTTPS location.

The problem is that if i connect to our server, retireve my
information and then get passed off to the thirdy party server it will
work the first time (most of the time). However if I were to click
the back button (or cancel on the screen) and go back to view my
information and then get passed off to the third party again I will
get "The operation has timed out". Running some more tests, I
determined I am getting a "System.Net.WebException"

I have found some possible solutions and tried some, but none have
worked. I am not even sure if the issue is on my end as the third
party is being a little slow in helping with this issue.

code that does the post is below, hoping someone sees something.

Thanks



private static void start_post(string strPage, string strBuffer,
string auth_user, string auth_pass)
{
//HttpContext.Current.Response.Write(strPage + "<br>");
//HttpContext.Current.Response.Write(strBuffer + "<br>");
//Our postvars
byte[] buffer = Encoding.ASCII.GetBytes(strBuffer);

//Initialisation
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create
(strPage);

string auth = auth_user + ":" + auth_pass;

byte[] authBytes = Encoding.UTF8.GetBytes(auth.ToCharArray());
//byte[] authBytes = Encoding.UTF8.GetBytes
("holmenm:IlpIhwN71".ToCharArray());
WebReq.Headers["Authorization"] = "Basic " +
Convert.ToBase64String(authBytes);
//WebReq.Headers.Add("Authorization", "Basic
dG9tY2F0OnRvbWNhdA==");

//Our method is post, otherwise the buffer (postvars) would be
useless
WebReq.Method = "POST";

//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";

//The length of the buffer (postvars) is used as
contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always
important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();

//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse
();
//Let's show some information about the response
HttpContext.Current.Response.Write(WebResp.StatusCode +
"<br>");
HttpContext.Current.Response.Write(WebResp.Server + "<br>");

//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
HttpContext.Current.Response.Write(_Answer.ReadToEnd() +
"<br>");

Answer.Close();
_Answer.Close();
WebResp.Close();
}
 
G

George

If you are getting exception then your HttpWebRequest, HttpWebResponse left
not closed.
by default you only can have 2 simultaneously open HTTP connections to the
same server, so the third one will wait till one of those 2 will be closed
and if not then it will timeout.

PS: You need to analyze WebException to find out why you getting
WebException. WebException has it's own stream object to you can analyze
response from server.

George.

Matt H. said:
I am having a problem with a HTTPWebrequest that I am posting to a
third party server. This is an ecommerce scenario where someone is
retrieving their bill information from our server and then we are
passing them off to a third party to collect the payment.

On our server they are in an HTTPS state, and we are passing them off
to another HTTPS location.

The problem is that if i connect to our server, retireve my
information and then get passed off to the thirdy party server it will
work the first time (most of the time). However if I were to click
the back button (or cancel on the screen) and go back to view my
information and then get passed off to the third party again I will
get "The operation has timed out". Running some more tests, I
determined I am getting a "System.Net.WebException"

I have found some possible solutions and tried some, but none have
worked. I am not even sure if the issue is on my end as the third
party is being a little slow in helping with this issue.

code that does the post is below, hoping someone sees something.

Thanks



private static void start_post(string strPage, string strBuffer,
string auth_user, string auth_pass)
{
//HttpContext.Current.Response.Write(strPage + "<br>");
//HttpContext.Current.Response.Write(strBuffer + "<br>");
//Our postvars
byte[] buffer = Encoding.ASCII.GetBytes(strBuffer);

//Initialisation
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create
(strPage);

string auth = auth_user + ":" + auth_pass;

byte[] authBytes = Encoding.UTF8.GetBytes(auth.ToCharArray());
//byte[] authBytes = Encoding.UTF8.GetBytes
("holmenm:IlpIhwN71".ToCharArray());
WebReq.Headers["Authorization"] = "Basic " +
Convert.ToBase64String(authBytes);
//WebReq.Headers.Add("Authorization", "Basic
dG9tY2F0OnRvbWNhdA==");

//Our method is post, otherwise the buffer (postvars) would be
useless
WebReq.Method = "POST";

//We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded";

//The length of the buffer (postvars) is used as
contentlength.
WebReq.ContentLength = buffer.Length;
//We open a stream for writing the postvars
Stream PostData = WebReq.GetRequestStream();
//Now we write, and afterwards, we close. Closing is always
important!
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();

//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse
();
//Let's show some information about the response
HttpContext.Current.Response.Write(WebResp.StatusCode +
"<br>");
HttpContext.Current.Response.Write(WebResp.Server + "<br>");

//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
HttpContext.Current.Response.Write(_Answer.ReadToEnd() +
"<br>");

Answer.Close();
_Answer.Close();
WebResp.Close();
}
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top