Solved

Setting timeout on WebClient

Posted on 2004-04-03
2
2,542 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

11 Experts available now in Live!

Get 1:1 Help Now