Dynotoe
asked on
C# - Webclient not working, perhaps a timeout issue
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
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);
}
Use HTTPWebRequest and HTTPWebResponse, it has an timeout property.
ASKER
Hi junges,
Could you show me how to do this?
Thanks.
-D
Could you show me how to do this?
Thanks.
-D
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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)
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)
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 );
System.IO.Stream rfile = resp.GetResponseStream();
int readed = 0;
do {
readed = rfile.Read( buffer, 0, 1024 );
// myLocalFile.Write( buffer, 0, readed );
} while ( readed != 0 );