Solved

Setting timeout on WebClient

Posted on 2004-04-03
2
2,540 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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now