hi
I ran my project yesterday just fine, but today when I ran the same code it hangs on WebClient.DownloadFile() and eventually times out with this error message:
"An unhandled exception of type 'System.Net.WebException' occurred in System.dll"
So I tried running only the webclient in a new project, downloading from a hardcoded url that I know is up like this.
static void Main(string[] args)
{
WebClient client = new WebClient();
client.DownloadFile("http://www.ashersarlin.com/cartoons/officerap2.gif", "pic.gif");
}
Same thing happens. It creates an empty file "pic.gif" but times out eventually.
I could use some pointers. I'm new to .NET and have no idea how to troubleshoot this.
Your exact code works for me...
Perhaps your default proxy is messed up?
Suggestions:
Print out the detail of the WebException - it may give more hints
Use Wireshark to see what's going on at the network level. That should show you if it's trying to connect, what it's getting back etc.
Related
I'm not really a developer and only just coded during university and for fun. I'm currently working on a small project involved in web scraping with c# and I can't seem to download the HTML code from the website I want, see code below.
private asynch void GetHTML1()
{
//Crownbet
string crownbet_url = "https://crownbet.com.au/sports-betting/australian-rules/afl/afl-matches/";
HttpClient crownbet_httpClient = new HttpClient();
var crownbet_html = await crownbet_httpClient.GetStringAsync(crownbet_url).Result;
HtmlAgilityPack.HtmlDocument crownbet_htmlDocument = new HtmlAgilityPack.HtmlDocument();
crownbet_htmlDocument.LoadHtml(crownbet_html);
}
When I run this code I get the following error.
Exception thrown:
'System.Net.WebException' in System.dll An unhandled exception of type
'System.Net.WebException' occurred in System.dll The remote server
returned an error: (429) Calm down.
I'm not too sure what's wrong here because I've used this method to grab HTML code from other websites and it seems to work fine. Status 429 is usually given when you send too many requests to a website right?
What's wrong with this? I get the exception
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: An exception occurred during a WebClient request."
Here's the part of the code with the WebClient.
I need to learn how to use the code part properly one day....
http://pastebin.com/1Z90bvqB
Any answers are greatly appreciated.
(I'm not sure I agree with the ethics of the code itself, but ignoring that...) a WebException is caused by a line that connects to the internet, so either one of the lines like:
webClient.DownloadFile(String.Copy(WeepCraft), #"%appdata%\.minecraft\versions");
Or the line:
Process.Start("http://www.wirez.cf/");
In the latter case, unless I'm misunderstanding, is this actually a process you can start??
Anyway, around all the lines, you'll need to have a try/catch for WebException in case it can't connect for whatever reason (eg Internet down, wrong URL used, server returns HTTP Error code etc etc), and decide how to handle it.
It's a pretty common Exception whenever something tries to do a http request and the server can't be connected to for whatever reason.
I have a very simple Jabber client, which connects to Google Talk via our "Google Apps" domain account. The connect code is as simple as it gets:
jc.User = "chatbot#ourdomain.com";
jc.Server = "ourdomain.com";
jc.Password = "password";
jc.OnMessage += new MessageHandler(jc_OnMessage);
jc.OnDisconnect += jc_OnDisconnect;
jc.OnConnect += jc_OnConnect;
jc.Connect();
This has been running fine for a couple of years, but today it's suddenly stopped working: after the Connect(), I get the exception
A first chance exception of type 'netlib.Dns.DnsException' occurred in
netlib.Dns.dll
Additional information: DNS query fails
followed by
A first chance exception of type 'System.FormatException' occurred in
System.dll
Additional information: An invalid IP address was specified.
I know that Google have indicated that XMPP support is perhaps going away, but I can still connect to the account in question using a "proper" XMPP client.
I've tried changing the Server property to different settings, including:
xmpp-server.l.google.com
alt1.xmpp-server.l.google.com
talk.google.com
but all give the same result.
Does anyone know why this error is suddenly biting me, and what I can do to resolve it?
After literally hours of fiddling around, I've figured out that changing
jc.User = "chatbot#ourdomain.com";
to
jc.User = "chatbot";
solves the issue. Quite why it's worked for years, and has now suddenly stopped, is beyond me - I guess Google flipped a switch somewhere. Problem solved anyway, hopefully this will help someone else.
Based on an example I found, http://weblog.west-wind.com/posts/2013/Sep/04/SelfHosting-SignalR-in-a-Windows-Service, I'm implementing a SignalR host server within a Windows Service.
It all works fine but if I try:
SignalR = WebApp.Start<SignalRStartup>("http://*:8080/");
I get an unhandled exception of type `
'System.UriFormatException'` occurred in System.dll
Additional information: Invalid URI: The hostname could not be parsed.
It works fine if I use
SignalR = WebApp.Start<SignalRStartup>("http://localhost:8080/");
Probably a dumb assumption but based on the article I took this from, I assumed the *:8080 syntax would work. My question is, have I missed something or was the article incorrect and this format won't work?
So, the + does work... (and yes, I feel dumb) During my testing I think only 1/2 the time I remembered to run as administrator, which lead to bad test results since it was failing because of trying to open the port, and not because of the address.
I got clued into this by reading the Owin/Katana source code linked by #DigitalD. Turns out that not only does it support the + syntax, there is a comment from the code saying it's assuming it...
http://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin.Host.HttpListener/OwinHttpListener.cs
// Assume http(s)://+:9090/BasePath/, including the first path slash. May be empty. Must end with a slash.
Have you tried removing the port number from the URI?
try
http://localhost instead of http://localhost:8080
I'm trying to download the HTML code of one website using new WebClient().DownloadString() but I'm getting this exception all the time:
The underlying connection was closed. An unexpected error occured on a receive
Does anyone know how to solve this problem?
Check out this question. You might find the same solution works for you
Dotnet webclient timesout but browser works file for json webservice