Solved

Setting timeout on WebClient

Posted on 2004-04-03
2
2,543 Views
Last Modified: 2008-02-01
I'm using a WebClient to request an http page.  When my internet connection is broken, this takes forever to timeout, is there an easy way to set the timeout or do I need to loop around it and check.
0
Comment
Question by:jjacksn
2 Comments
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
ID: 10750803
The WebClient unfortunately seems to hide the timeout property on the underlying WebRequest object.  If you want to set a timeout you will need to use the HttpWebRequest object itself.  

It's pretty straightforward... (This does a post, you can change the method to a get too)

public string postToURL(string sURL, string sData)
          {
               HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(sURL);
               httpReq.Method = "POST";
               httpReq.ContentType = "application/x-www-form-urlencoded";
               httpReq.ContentLength = sData.Length;
               httpReq.Timeout = 3000; //3 seconds
               Stream st = httpReq.GetRequestStream();

               StreamWriter stw = new StreamWriter(st);
               stw.Write(sData);

               stw.Flush();
               stw.Close();

        HttpWebResponse httpRes = httpReq.GetResponse();
       StreamReader sr = new StreamReader(httpRes.GetResponseStream());
        string result = sr.ReadToEnd();
        sr.Close();

  return result;
}


HTH
0
 
LVL 9

Expert Comment

by:Joeisanerd
ID: 10750806
Try setting the socket options

// socket declared elsewhere

socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question