473,795 Members | 3,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is this HttpWebRequest class unreliable?

Hi,

Could someone please give me some suggestions on how to make this class
robust. I need to be able to handle around 20 similtanious requests to
this class which causes a web browser to display a waiting message
while it requests XML data from a third party server. Some requests can
take around 30 seconds.

Usually the first time I do the request it always works. If I try again
without closing the web browser it will fail sometimes. And if I try
from more than 1 web browser at the same time it will fail a lot.

bool CheckAvailable( ) is used for the requests.

Thanks
using System;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
using System.Threadin g;

namespace TestPost
{
/// <summary>
/// Summary description for SamplePost.
/// </summary>
public class SamplePost
{
public static AutoResetEvent m_allDone = null;

public SamplePost()
{
}

private bool CheckAvailable( string strURL, string strXMLPost,stri ng
strUser, string strPassword)
{
// send POST request
m_strURL = strURL;
m_strXMLPost = strXMLPost;
m_strUser = strUser;
m_strPassword = strPassword;

RequestState myRequestState = null;
// send POST request
try
{
// Create a new 'HttpWebRequest ' object to the mentioned URL.
HttpWebRequest myHttpWebReques t1 =
(HttpWebRequest )WebRequest.Cre ate(m_strURL);
myRequestState = new RequestState();
myRequestState. request = myHttpWebReques t1;
myRequestState. m_strPostData = m_strXMLPost;
myHttpWebReques t1.ContentType =
"applicatio n/x-www-form-urlencoded";
myHttpWebReques t1.Method = "POST";
myHttpWebReques t1.Headers.Add( "Cache-Control","no-cache");
myHttpWebReques t1.ContentLengt h = m_strXMLPost.Le ngth;
myHttpWebReques t1.KeepAlive = true;
myHttpWebReques t1.Credentials = new
NetworkCredenti al(m_strUser,m_ strPassword);

m_allDone = new AutoResetEvent( false);
IAsyncResult result = (IAsyncResult)
myHttpWebReques t1.BeginGetRequ estStream(new
AsyncCallback(P ostCallback),my RequestState);
// using result.AsyncWai tHandle slips past WaitOne, so trying
threadpool
// result.AsyncWai tHandle.WaitOne (20000,true);
ThreadPool.Regi sterWaitForSing leObject (result.AsyncWa itHandle, new
WaitOrTimerCall back(TimeoutSoc ketCallback), myHttpWebReques t1,
DefaultTimeout, true);
m_allDone.WaitO ne();

if(!myRequestSt ate.m_bResultOK )
return false;

// Start the asynchronous BeginGetRespons e request.
m_allDone = new AutoResetEvent( false);
IAsyncResult result2 = (IAsyncResult)
myHttpWebReques t1.BeginGetResp onse(new
AsyncCallback(R espCallback),my RequestState);
ThreadPool.Regi sterWaitForSing leObject (result2.AsyncW aitHandle,
new WaitOrTimerCall back(TimeoutSoc ketCallback), myHttpWebReques t1,
Defines.N_HTTP_ REQUEST_TIMEOUT , true);
m_allDone.WaitO ne();
myRequestState. response.Close( );
}
catch(WebExcept ion)
{
return false;
}
catch(Exception )
{
return false;
}
// check reply OK
bool bOK = false;
if(myRequestSta te != null)
{
string strReply = myRequestState. m_strReply;
bOK = VerifyXMLReply( strReply);
}
return bOK;
}

// send post data
private void PostCallback(IA syncResult asynchronousRes ult)
{
RequestState myRequestState = null;
try
{
// Set the State of request to asynchronous.
myRequestState = (RequestState)a synchronousResu lt.AsyncState;
HttpWebRequest myHttpWebReques t2 =
(HttpWebRequest )myRequestState .request;
// End of the Asynchronus writing .
Stream postStream =
myHttpWebReques t2.EndGetReques tStream(asynchr onousResult);
// send any post data
if(!Helpers.IsE mpty(myRequestS tate.m_strPostD ata))
{
ASCIIEncoding encoder = new ASCIIEncoding() ;
// Convert the string into byte array.
byte[] ByteArray = encoder.GetByte s(myRequestStat e.m_strPostData );
// Write to the stream.
postStream.Writ e(ByteArray,0,m yRequestState.m _strPostData.Le ngth);
}
postStream.Clos e();
myRequestState. m_bResultOK = true;
m_allDone.Set() ;
}
catch(WebExcept ion e)
{
if(myRequestSta te != null)
{
myRequestState. m_strErrorText = e.Message;
myRequestState. m_bResultOK = false;
}
}
catch(Exception e)
{
if(myRequestSta te != null)
{
myRequestState. m_strErrorText = e.Message;
myRequestState. m_bResultOK = false;
}
}
m_allDone.Set() ;
}

// read response
private void RespCallback(IA syncResult asynchronousRes ult)
{
RequestState myRequestState = null;
try
{
// State of request is asynchronous.
myRequestState = (RequestState) asynchronousRes ult.AsyncState;
HttpWebRequest myHttpWebReques t = myRequestState. request;
myRequestState. response = (HttpWebRespons e)
myHttpWebReques t.EndGetRespons e(asynchronousR esult);
// Read the response into a Stream object.
Stream responseStream =
myRequestState. response.GetRes ponseStream();
myRequestState. streamResponse = responseStream;
// Begin the Reading of the contents of the HTML page and print it
to the console.
IAsyncResult asynchronousInp utRead =
responseStream. BeginRead(myReq uestState.Buffe rRead, 0, BUFFER_SIZE, new
AsyncCallback(R eadCallBack), myRequestState) ;
return;
}
catch(WebExcept ion e)
{
if(myRequestSta te != null)
{
myRequestState. m_bResultOK = false;
myRequestState. m_strErrorText = e.Message;
}
}
m_allDone.Set() ;
}

// read callback
private void ReadCallBack(IA syncResult asyncResult)
{
RequestState myRequestState = null;
try
{
myRequestState = (RequestState)a syncResult.Asyn cState;
Stream responseStream = myRequestState. streamResponse;
int read = responseStream. EndRead(asyncRe sult);
// Read the HTML page and then print it to the console.
if (read > 0)
{
myRequestState. m_strReply +=
Encoding.ASCII. GetString(myReq uestState.Buffe rRead, 0, read);
IAsyncResult asynchronousRes ult = responseStream. BeginRead(
myRequestState. BufferRead, 0, BUFFER_SIZE, new
AsyncCallback(R eadCallBack), myRequestState) ;
return;
}
else
{
myRequestState. m_bResultOK = true;
responseStream. Close();
}
}
catch(WebExcept ion e)
{
if(myRequestSta te != null)
{
myRequestState. m_bResultOK = false;
myRequestState. m_strErrorText = e.Message;
}
}
m_allDone.Set() ;
}

// Abort the request if the timer fires.
private void TimeoutSocketCa llback(object state, bool timedOut)
{
if(timedOut)
{
HttpWebRequest request = state as HttpWebRequest;
if (request != null)
{
request.Abort() ;
}
}
}
}

// used for asyncronous requests
public class RequestState
{
// This class stores the State of the request.
const int BUFFER_SIZE = 1024;
public string m_strPostData;
public string m_strReply;
public string m_strErrorText;
public bool m_bResultOK;
public byte[] BufferRead;
public HttpWebRequest request;
public HttpWebResponse response;
public Stream streamResponse;
// constructor
public RequestState()
{
BufferRead = new byte[BUFFER_SIZE];
request = null;
streamResponse = null;
m_bResultOK = false;
m_strPostData = "";
m_strErrorText = "";
m_strReply = "";
}
}
}

Nov 19 '05 #1
1 2611
Hi sfoxover:

You have several threading issues in this code. For example,
declaring AutoResetEvent as a static member is begging for your
threads to miss the signal entirely.

My suggestion would be to get rid of all the asynch activity - it's
not buying you anything in this code. The original thread is doing
nothing but waiting for the second thread to finish - so why not skip
the 2nd thread and do all the work with 1?

Try to get it working first, and then see if you need to make some
optimizations.

You'll also want to bump up the number of HTTP connections you can
make to an external machine:
http://odetocode.com/Blogs/scott/arc...06/08/272.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 11 Jul 2005 23:27:55 -0700, sf******@gmail. com wrote:
Hi,

Could someone please give me some suggestions on how to make this class
robust. I need to be able to handle around 20 similtanious requests to
this class which causes a web browser to display a waiting message
while it requests XML data from a third party server. Some requests can
take around 30 seconds.

Usually the first time I do the request it always works. If I try again
without closing the web browser it will fail sometimes. And if I try
from more than 1 web browser at the same time it will fail a lot.

bool CheckAvailable( ) is used for the requests.

Thanks
using System;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
using System.Threadin g;

namespace TestPost
{
/// <summary>
/// Summary description for SamplePost.
/// </summary>
public class SamplePost
{
public static AutoResetEvent m_allDone = null;

public SamplePost()
{
}

private bool CheckAvailable( string strURL, string strXMLPost,stri ng
strUser, string strPassword)
{
// send POST request
m_strURL = strURL;
m_strXMLPost = strXMLPost;
m_strUser = strUser;
m_strPassword = strPassword;

RequestState myRequestState = null;
// send POST request
try
{
// Create a new 'HttpWebRequest ' object to the mentioned URL.
HttpWebRequest myHttpWebReques t1 =
(HttpWebReques t)WebRequest.Cr eate(m_strURL);
myRequestState = new RequestState();
myRequestState. request = myHttpWebReques t1;
myRequestState. m_strPostData = m_strXMLPost;
myHttpWebReques t1.ContentType =
"applicatio n/x-www-form-urlencoded";
myHttpWebReques t1.Method = "POST";
myHttpWebReques t1.Headers.Add( "Cache-Control","no-cache");
myHttpWebReques t1.ContentLengt h = m_strXMLPost.Le ngth;
myHttpWebReques t1.KeepAlive = true;
myHttpWebReques t1.Credentials = new
NetworkCredent ial(m_strUser,m _strPassword);

m_allDone = new AutoResetEvent( false);
IAsyncResult result = (IAsyncResult)
myHttpWebReque st1.BeginGetReq uestStream(new
AsyncCallback( PostCallback),m yRequestState);
// using result.AsyncWai tHandle slips past WaitOne, so trying
threadpool
// result.AsyncWai tHandle.WaitOne (20000,true);
ThreadPool.Regi sterWaitForSing leObject (result.AsyncWa itHandle, new
WaitOrTimerCal lback(TimeoutSo cketCallback), myHttpWebReques t1,
DefaultTimeout , true);
m_allDone.WaitO ne();

if(!myRequestSt ate.m_bResultOK )
return false;

// Start the asynchronous BeginGetRespons e request.
m_allDone = new AutoResetEvent( false);
IAsyncResult result2 = (IAsyncResult)
myHttpWebReque st1.BeginGetRes ponse(new
AsyncCallback( RespCallback),m yRequestState);
ThreadPool.Regi sterWaitForSing leObject (result2.AsyncW aitHandle,
new WaitOrTimerCall back(TimeoutSoc ketCallback), myHttpWebReques t1,
Defines.N_HTTP _REQUEST_TIMEOU T, true);
m_allDone.WaitO ne();
myRequestState. response.Close( );
}
catch(WebExcept ion)
{
return false;
}
catch(Exception )
{
return false;
}
// check reply OK
bool bOK = false;
if(myRequestSta te != null)
{
string strReply = myRequestState. m_strReply;
bOK = VerifyXMLReply( strReply);
}
return bOK;
}

// send post data
private void PostCallback(IA syncResult asynchronousRes ult)
{
RequestState myRequestState = null;
try
{
// Set the State of request to asynchronous.
myRequestState = (RequestState)a synchronousResu lt.AsyncState;
HttpWebRequest myHttpWebReques t2 =
(HttpWebReques t)myRequestStat e.request;
// End of the Asynchronus writing .
Stream postStream =
myHttpWebReque st2.EndGetReque stStream(asynch ronousResult);
// send any post data
if(!Helpers.IsE mpty(myRequestS tate.m_strPostD ata))
{
ASCIIEncoding encoder = new ASCIIEncoding() ;
// Convert the string into byte array.
byte[] ByteArray = encoder.GetByte s(myRequestStat e.m_strPostData );
// Write to the stream.
postStream.Writ e(ByteArray,0,m yRequestState.m _strPostData.Le ngth);
}
postStream.Clos e();
myRequestState. m_bResultOK = true;
m_allDone.Set() ;
}
catch(WebExcept ion e)
{
if(myRequestSta te != null)
{
myRequestState. m_strErrorText = e.Message;
myRequestState. m_bResultOK = false;
}
}
catch(Exception e)
{
if(myRequestSta te != null)
{
myRequestState. m_strErrorText = e.Message;
myRequestState. m_bResultOK = false;
}
}
m_allDone.Set() ;
}

// read response
private void RespCallback(IA syncResult asynchronousRes ult)
{
RequestState myRequestState = null;
try
{
// State of request is asynchronous.
myRequestState = (RequestState) asynchronousRes ult.AsyncState;
HttpWebRequest myHttpWebReques t = myRequestState. request;
myRequestState. response = (HttpWebRespons e)
myHttpWebReque st.EndGetRespon se(asynchronous Result);
// Read the response into a Stream object.
Stream responseStream =
myRequestState .response.GetRe sponseStream();
myRequestState. streamResponse = responseStream;
// Begin the Reading of the contents of the HTML page and print it
to the console.
IAsyncResult asynchronousInp utRead =
responseStream .BeginRead(myRe questState.Buff erRead, 0, BUFFER_SIZE, new
AsyncCallback( ReadCallBack), myRequestState) ;
return;
}
catch(WebExcept ion e)
{
if(myRequestSta te != null)
{
myRequestState. m_bResultOK = false;
myRequestState. m_strErrorText = e.Message;
}
}
m_allDone.Set() ;
}

// read callback
private void ReadCallBack(IA syncResult asyncResult)
{
RequestState myRequestState = null;
try
{
myRequestState = (RequestState)a syncResult.Asyn cState;
Stream responseStream = myRequestState. streamResponse;
int read = responseStream. EndRead(asyncRe sult);
// Read the HTML page and then print it to the console.
if (read > 0)
{
myRequestState. m_strReply +=
Encoding.ASCII .GetString(myRe questState.Buff erRead, 0, read);
IAsyncResult asynchronousRes ult = responseStream. BeginRead(
myRequestState .BufferRead, 0, BUFFER_SIZE, new
AsyncCallback( ReadCallBack), myRequestState) ;
return;
}
else
{
myRequestState. m_bResultOK = true;
responseStream. Close();
}
}
catch(WebExcept ion e)
{
if(myRequestSta te != null)
{
myRequestState. m_bResultOK = false;
myRequestState. m_strErrorText = e.Message;
}
}
m_allDone.Set() ;
}

// Abort the request if the timer fires.
private void TimeoutSocketCa llback(object state, bool timedOut)
{
if(timedOut)
{
HttpWebRequest request = state as HttpWebRequest;
if (request != null)
{
request.Abort() ;
}
}
}
}

// used for asyncronous requests
public class RequestState
{
// This class stores the State of the request.
const int BUFFER_SIZE = 1024;
public string m_strPostData;
public string m_strReply;
public string m_strErrorText;
public bool m_bResultOK;
public byte[] BufferRead;
public HttpWebRequest request;
public HttpWebResponse response;
public Stream streamResponse;
// constructor
public RequestState()
{
BufferRead = new byte[BUFFER_SIZE];
request = null;
streamResponse = null;
m_bResultOK = false;
m_strPostData = "";
m_strErrorText = "";
m_strReply = "";
}
}
}


Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1313
by: Krzysztof Kazmierczak | last post by:
Hi All! I'm sending a web request (using HttpWebRequest class) to the page which contains frameset. I have such a response: Your browser cannot display frames (i'm using IE 6.0). What can i do to get a proper response from that site? Maybe i have to set userAgent property of HttpWebRequest class? Thanks for any help!
10
472
by: Brian Brown | last post by:
I have code which works as an asp.net page that posts an xml file to web page and gets a response back. When the the calls GetResponse() it goes into the page it's posting to to and works fine. When it's been ported to a winform it doesn't work on the GetResponse() call. I think it probably needs credentials but not sure what to use, tried a few ids w/o any luck.
8
1589
by: Greg Bacchus | last post by:
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of the class that is calling it. e.g. class A { public A() {
2
5457
by: Mark Rae | last post by:
Hi, Can anyone please tell me if it's possible to use HttpWebRequest and HttpWebResponse in a class in a Windows application? I've tried referencing System and System.Web but there's still something missing. Any assistance gratefully received. Best,
11
15896
by: Keith Patrick | last post by:
Could someone explain to me the relationship between these two classes? I am ripping my hair out trying to divert an HttpRequest to a new location via an HttpWebRequest, but I cannot get my session xfer to work, possibly due to the cookies not being compatible. I've spent over a week trying to get the HWR to integrate nicely with my app, but I cannot get the session to transfer, and it'd probably take me even longer if I tried to digest...
1
1634
by: iana_kosio | last post by:
Hi, I am using HttpWebRequest class to communicate with remote server. In some cases the server would return 5xx status code which results in HttpWebRequest object throwing an exception. I, however, still need to access the response stream from the server as it contains information that I need. Any ideas how I can do that? Thanks, Konstantin try { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/myUrl");
2
6048
by: hharry | last post by:
hello all, trying to consume a simple web service using httpwebrequest instead of generating a proxy class. code for simple web service: Imports System.Web.Services <System.Web.Services.WebService(Namespace :=
15
3100
by: Nightcrawler | last post by:
I am currently using the HttpWebRequest and HttpWebResponse to pull webpages down from a few urls. string url = "some url"; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse()) {
3
1929
by: wzb6 | last post by:
Hi, I am trying to post form values to a https web page programmatically using Httpwebrequest but no matter what I do the same login page is returned instead of the next page. I would very much appreciate if someone could show me what is it that I am doing wrong. Below is the code that I am using. Imports System.IO
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.