webexception

System.Net.WebException HTTP status code

拟墨画扇 提交于 2019-11-27 18:03:35
Is there an easy way to get the HTTP status code from a System.Net.WebException ? Maybe something like this... try { // ... } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { var response = ex.Response as HttpWebResponse; if (response != null) { Console.WriteLine("HTTP Status Code: " + (int)response.StatusCode); } else { // no http status code available } } else { // no http status code available } } Martin Liversage By using the null-conditional operator ( ?. ) you can get the HTTP status code with a single line of code: HttpStatusCode? status = (ex.Response as

Handling two WebException's properly

大憨熊 提交于 2019-11-27 15:51:25
问题 I am trying to handle two different WebException 's properly. Basically they are handled after calling WebClient.DownloadFile(string address, string fileName) AFAIK, so far there are two I have to handle, both WebException 's: The remote name could not be resolved (i.e. No network connectivity to access server to download file) (404) File not nound (i.e. the file doesn't exist on the server) There may be more but this is what I've found most important so far. So how should I handle this

System.Net.WebException HTTP status code

Deadly 提交于 2019-11-26 19:16:20
问题 Is there an easy way to get the HTTP status code from a System.Net.WebException ? 回答1: Maybe something like this... try { // ... } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { var response = ex.Response as HttpWebResponse; if (response != null) { Console.WriteLine("HTTP Status Code: " + (int)response.StatusCode); } else { // no http status code available } } else { // no http status code available } } 回答2: By using the null-conditional operator ( ?. ) you can

WebException how to get whole response with a body?

点点圈 提交于 2019-11-26 15:16:12
In WebException I cannot see body of GetResponse. This is my code in C#: try { return GetResponse(url + "." + ext.ToString(), method, headers, bodyParams); } catch (WebException ex) { switch (ex.Status) { case WebExceptionStatus.ConnectFailure: throw new ConnectionException(); case WebExceptionStatus.Timeout: throw new RequestTimeRanOutException(); case WebExceptionStatus.NameResolutionFailure: throw new ConnectionException(); case WebExceptionStatus.ProtocolError: if (ex.Message == "The remote server returned an error: (401) unauthorized.") { throw new CredentialsOrPortalException(); } throw