is seems to me that WebRequest.Create(url) fails a litle to soon..
Explanations:
on a url that it failed (or throwed a System.Net.WebException) ... copy-paste -ing that url into the browser seems to work..browser gives response!.. sometimes with a remarcable delay (~10 seconds).. but WebRequest in less than 3 seconds throws exception
Example of valid url on which it fails:
http://tracker.podtropolis.com:2710/announce?info_hash=%92%FD%2F%0B%40%F64%C5%86%19%D6%3E%B1%28%B2%81%A1J%D4%F6&peer_id=-AZ2060-%AD%18%05o%11%A6%26%B3%C3%D16%AC&port=6881&downloaded=0&uploaded=0&left=647749313&numwant=30&compact=1&event=started
if it has any meaning firefox reacts 10 times more slowly on this url that explorer, also sometimes firefox crashes on loading such a url
So question Why is WebRequest failing so soon?? I would like it to try a litle harder to get response from URL...
And this is the method that catches exeption (here i check if url is valid OR ~ "is tracker alive??")
public static bool isURLValid(string url)
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//Returns TURE if the Status code == 200
return (response.StatusCode == HttpStatusCode.OK);
}
catch (Exception ex) //(WebException ex)
{
Logger.e(TAG, "isURLValid", ex);
return false; //Any exception will returns false.
}
}
if valid i get stream (i know .. I know ..double contact.. but still)
inputStream = WebRequest.Create(fullURL).GetResponse().GetResponseStream();
So.. thoughts?
Related
var url1 = "https://www.regulations.gov/contentStreamer?documentId=BIS-2018-0006-16235&attachmentNumber=2&contentType=pdf"; // url exist but issue when downloading
var url2 = "https://www.regulations.gov/contentStreamer?documentId=BIS-2018-0006-16010&attachmentNumber=2&contentType=pdf"; // url does not exist
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
response.Close();
}
catch (WebException ex)
{
// i am getting http 500 server error exception when trying to see if both urls exist.
}
Can anyone help me to figure out a better way to get response when url does not exist and when there is a issue downloading url.
I want to download one image from url using console application.
I have used following code:
string sourceUrl = "http://i.ytimg.com/vi/pvBnYBsUi9A/default.jpg"; // Not Found
//string sourceUrl = "http://i.ytimg.com/vi/OrxZAN1FZUY/default.jpg"; // Found
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sourceUrl);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (Exception)
{
}
Above code throws exception in line "response = (HttpWebResponse)request.GetResponse();"
but when I am accessing "http://i.ytimg.com/vi/pvBnYBsUi9A/default.jpg" url in my browser then image will be display.
What I am missing here?
I tried that url "http://i.ytimg.com/vi/pvBnYBsUi9A/default.jpg" in Chrome
developer tools. It also receives a 404, but the response includes the image, which displays.
Your code is not the cause of the exception. The site is returning a 404 and your code gets an exception.
You could write logic to look at the response even if you get a 404 and decide whether to take it anyway, as the browser does.
It looks like you can get the response returned by the site if you catch WebException, which allows you to see the http request status and the response, per the documentation.
Example from the .Net 4.5 doc...
try
{
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for a response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}",
myHttpWebResponse.StatusDescription);
// Releases the resources of the response.
myHttpWebResponse.Close();
}
catch(WebException e)
{
Console.WriteLine("\r\nWebException Raised. The following error occured : {0}",e.Status);
}
catch(Exception e)
{
Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
WebException has Response and Status properties. So it looks like the .Net way to deal with this is to catch WebException and determine how to handle based on the status and response content (if necessary).
I am writing code to surf a website using spring webflow.
I have to update my cookie with his set-cookie header every request else my session will be terminated.
Sometime a request could bump into webException so I need to catch the webException response headers for the cookie update so my next request is still valid
The problem happens with timeout exception, other exceptions like 503 works fine. The webRespones is always null but I can see the server had respond the header part and only stuck in sending the body part in fiddler. My session will be terminated without catching the header response properly. Is there any work around for this?
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://example.com");
webRequest.CookieContainer = new CookieContainer();
webRequest.CookieContainer.Add(cookieCollection);
webRequest.Timeout = 20000;
webRequest.ReadWriteTimeout = 20000;
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
//A function handling the cookie adding
AddCookies(webResponse.Headers, cookieCollection, webResponse.ResponseUri.Host);
}
}
catch (WebException ex)
{
HttpWebResponse webResponse = ex.Response as HttpWebResponse;
if (webResponse == null) throw ex;
//Null webResponse when timeout, the received header information is dropped.
AddCookies(webResponse.Headers, cookieCollection, webResponse.ResponseUri.Host);
}
The timeout exception is implemented on the client side so the response will be null. You could use raw sockets to implement the get, but the easiest solution would probably be to increase the timeout value so you get the response.
I am writing an application to check the status of some internal web applications. Some of these applications use Windows authentication. When I use this code to check the status, it throws The remote server returned an error: (401) Unauthorized.. Which is understandable because I haven't provided any credentials to the webiste so I am not authorized.
WebResponse objResponse = null;
WebRequest objRequest = HttpWebRequest.Create(website);
objResponse = objRequest.GetResponse();
Is there a way to ignore the 401 error without doing something like this?
WebRequest objRequest = HttpWebRequest.Create(website);
try
{
objResponse = objRequest.GetResponse();
}
catch (WebException ex)
{
//Catch and ignore 401 Unauthorized errors because this means the site is up, the app just doesn't have authorization to use it.
if (!ex.Message.Contains("The remote server returned an error: (401) Unauthorized."))
{
throw;
}
}
I would suggest to try this:
try
{
objResponse = objRequest.GetResponse() as HttpWebResponse;
}
catch (WebException ex)
{
objResponse = ex.Response as HttpWebResponse;
}
finally
The WebException has the response all information you want.
When the server is down or unreachable you will get a timeout exception. I know that the only way to handle that is with a try/catch.
I'm quite sure this is the case for most errors (401/404/501), so: No, you can't ignore (prevent) the exceptions but you will have to handle them. They are the only way to get most of the StatusCodes your App is looking for.
The short of it is you'll want to check the myHttpWebResponse.StatusCode for the status code and act accordingly.
Sample code from reference:
public static void GetPage(String url)
{
try
{
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for a response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}",
myHttpWebResponse.StatusDescription);
// Releases the resources of the response.
myHttpWebResponse.Close();
}
catch(WebException e)
{
Console.WriteLine("\r\nWebException Raised. The following error occured : {0}",e.Status);
}
catch(Exception e)
{
Console.WriteLine("\nThe following Exception was raised : {0}",e.Message);
}
}
My Question:
Does a HttpWebResponse.StatusCode detect Asp.Net errors? Mainly a yellow screen of death?
Some Background:
I am working on a simple c# console app that will test servers and services to make sure they are still functioning properly. I assumed since the HttpStatusCodes are enumerated with OK, Moved, InteralServerError, etc... that I could simply do the following.
WebRequest request = WebRequest.Create(url);
request.Timeout = 10000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null || response.StatusCode != HttpStatusCode.OK)
{
// SERVER IS OK
return false;
}
else
{
// SERVER HAS SOME PROBLEMS
return true;
}
I found out this morning however that this doesn't work. An ASP.Net application crashed showing a Yellow Screen Of Death and my application didn't seem to mind because the response.StatusCode equaled HttpStatusCode.OK.
What am I missing?
Thanks
// lance
Update
Thanks to Jon this seems to be working.
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException webexp)
{
response = (HttpWebResponse)webexp.Response;
}
GetResponse will throw a WebException for errors - but you can catch the WebException, use WebException.Response to get the response, and then get the status code from that.
As far as I'm aware, GetResponse never returns null, so you can remove that test from your code.
Also, rather than having if/else blocks to return true/false, it's simple just to return the result of evaluating an expression, for example:
return response.StatusCode == HttpStatusCode.OK;
(To be honest, you could probably return false if any WebException is thrown...)