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

G

Guest

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

Guest

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.
 
G

Guest

Thank you for your reply. Could you give me coding sample how to use
httpwebrequest and set the time-out property?
 
G

Guest

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>;
 
G

Guest

Thank you very much for the reply. Could you give me coding sample on getting
the response. I have difficulty in getting the response.
 
G

Guest

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();
 
G

Guest

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?
 
G

Guest

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.
 

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