473,837 Members | 1,726 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 2616
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
1315
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
1591
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
5460
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
15902
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
1639
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
6049
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
3102
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
1930
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
9846
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
9683
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,...
0
10579
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10633
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10276
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9412
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7007
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
5852
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3126
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.