How to control the time-out of a webclient request?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using webclient to retrieve data from a web site. How do I control the
time-out
of this request?
 
Hi,

The WebClient class does not offer any property where u can set the timeout
period. If possible use the HttpWebrequest instead. The Webclient insternally
uses the webrequest class. In HttpWebrequest you can set the request timeout
property.
 
Thank you for your reply. Could you give me coding sample how to use
httpwebrequest and set the time-out property?
 
Hi,

Here is a sample code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Endpoint);
//Endpoint is the URL to which u are making the request.
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
request.ContentType = "text/xml"; //depending on the data u are sending this
will change.
request.Timeout=<value in milliseconds>;
 
Thank you very much for the reply. Could you give me coding sample on getting
the response. I have difficulty in getting the response.
 
Hi,

Here is how u get the response.

try
{
// send the request and get the response
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
// handle exception
}
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string responseText = streamReader.ReadToEnd();
response.Close();
 
I am still not getting the response. I think the problem has something to do
with
the way of making the request.

I have a uriString which looks like

uriString =
S"http://trade1.masterlink.com.tw/fut...1&pwd=1234567890&idnoR11111111s&isPreorder=NO"

I pass the urlString to webclient

myWebClient->UploadData(uriString, postArray);

I get the response back without any problem.

Now I pass the same string to HttpWebrequest

HttpWebRequest *request =
dynamic_cast<HttpWebRequest*> (WebRequest::Create(uriString));

However I do not get any reponse and get time-out exception.

What is wrong here?
 
Hi,

Can u give the complete code...I tried that URL on the browser and the
browser is giving 500 internal error. So it means that prior to this u need
to do some other things also like logging in etc.
 
Back
Top