Solved

C# - Webclient not working, perhaps a timeout issue

Posted on 2009-04-06
5
1,848 Views
Last Modified: 2012-05-06
Hi everyone,

I have made a simple webclient to download data from a website.  It is hit or miss becasue when the returned data takes a while I don't get the data but rather a timeout.  The data from what I understand is being retrieved but very slowely some times.

Can someone please show me an alternative or how to configure the timeout settings or persistancy of the webclient?  My coding skills are failry limited so if you could show the solution coded it would be very helpful.

Please let me extend my gracious thank you in advance.

Cheers - Dynotoe
try
			{ 
				// Get HTML data 
				System.Net.WebClient client = new System.Net.WebClient();
								Stream data = client.OpenRead(URL);	//Seems to break here and send to "catch"  Time out?
				// Also client.dDownloadFile(URL, "C:\\temp.asp"/* save file */);
				StreamReader reader = new StreamReader(data);
				string inputLine = "";
 
				statusBar1.Text = "Downloading data for  " + symbol;
 
				while( (inputLine = reader.ReadLine()) != null )
				{
					if(inputLine != "")
					{
						lineCount++;
						result.Add( inputLine.Replace(";",",") );
					}
				} 
				data.Close();
				if(lineCount < 18200)
				{
					richTextBox1.AppendText("  WARNING  " );
					richTextBox1.Select();		// Scrolls text box to end.
				}
			}
			catch(Exception ex)
			{
				Exception tempEx = ex;
				while(tempEx!=null)
				{
					richTextBox1.AppendText(tempEx.Message + "\r\n");
					tempEx=tempEx.InnerException;
				}
 
				lineCount = 0;
				richTextBox1.AppendText("      Failure.  New Attempt...  ");
				richTextBox1.Select();		// Scrolls text box to end.
				PauseForMilliSeconds( failureAndLowCountDelay );
				return(false);
			}

Open in new window

0
Comment
Question by:Dynotoe
[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
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 24085805
Use HTTPWebRequest and HTTPWebResponse, it has an timeout property.
0
 

Author Comment

by:Dynotoe
ID: 24086433
Hi junges,

Could you show me how to do this?

Thanks.

-D
0
 
LVL 14

Accepted Solution

by:
Daniel Junges earned 500 total points
ID: 24087176

System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create( "http://maps.weather.com/web/radar/us_orl_ultraradar_large_usen.jpg" );
System.Net.HttpWebResponse ws = (System.Net.HttpWebResponse)wr.GetResponse();
System.IO.Stream str = ws.GetResponseStream();
byte[] inBuf = new byte[100000];
int bytesToRead = (int)inBuf.Length;
int bytesRead = 0;
while ( bytesToRead > 0 ) {
    int n = str.Read( inBuf, bytesRead, bytesToRead );
    if ( n == 0 )
        break;
    bytesRead += n;
    bytesToRead -= n;
}
System.IO.FileStream fstr = new System.IO.FileStream( "weather.jpg", System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.Write );
fstr.Write( inBuf, 0, bytesRead );
str.Close();
fstr.Close();

Open in new window

0
 

Author Comment

by:Dynotoe
ID: 24087299
Thanks junges!

Quick question regarding the bytes to read, does this require me to know the size of the data before hand because it is actually different all the time?  Also is there a default time out that I need to lengthen or does this not time out at all?

Cheers - Dyno
aka Sean (Boston)
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 24087708
you can do the follow:

System.IO.Stream rfile = resp.GetResponseStream();

int readed = 0;
do {
    readed = rfile.Read( buffer, 0, 1024 );
    // myLocalFile.Write( buffer, 0, readed );  
} while ( readed != 0 );
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

726 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