Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting timeout on WebClient

Posted on 2004-04-03
2
Medium Priority
?
2,548 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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