Solved

Setting timeout on WebClient

Posted on 2004-04-03
2
2,545 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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