Check URL is working or not using c#.net - c#

while i am check URL using Web request and web Response method.
bool result = false;
try
{
if (!url.Contains("http://")) url = "http://" + url;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{ if (response.StatusCode == HttpStatusCode.OK) result = true; }
}
catch
{
return result;
}
return result;
But while i am check into this URL "http://payments.rctrustee.org/" am getting error. so i get the result is false. But i check into browser this URL is working.
i need this result true; how can i change my code.

Check with the following code:
try
{
if (!url.Contains("http://")) url = "http://" + url;
WebRequest req = WebRequest.Create(url);
WebResponse res = req.GetResponse();
Console.WriteLine("Url Exists");
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
if (ex.Message.Contains("remote name could not be resolved"))
{
Console.WriteLine("Url is Invalid");
}
}

Related

How to get html content from amazon using HttpWebRequest

I am trying to get HTML content from the amazon website. Here is my code to create request, response, and get string:
public static HttpWebResponse GetHttpWebResponse(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ContentType = "text/xml";
try
{
return (HttpWebResponse)webRequest.GetResponse();
}
catch (WebException e)
{
if (e.Response == null)
throw new Exception("Cannot get response");
return (HttpWebResponse)e.Response;
}
}
public static string GetString(HttpWebResponse response)
{
Encoding encoding = Encoding.UTF8;
using (var reader = new StreamReader(response.GetResponseStream(), encoding))
{
string responseText = reader.ReadToEnd();
return responseText;
}
}
It is working fine with other web sites. However, when I try to get content from amazon, for example:
https://www.amazon.com/gp/product/B00AEISSHA/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1
I am seeing encoded content:
I tried to change Encoding and used HttpUtility.HtmlDecode(html); but it couldn't help. Is there any simple way to get content from Amazon?
You're not catering for compression. If you update your webrequest like this, it should do the trick.
public static HttpWebResponse GetHttpWebResponse(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ContentType = "text/xml";
webRequest.AutomaticDecompression = DecompressionMethods.GZip;
try
{
return (HttpWebResponse)webRequest.GetResponse();
}
catch (WebException e)
{
if (e.Response == null)
throw new Exception("Cannot get response");
return (HttpWebResponse)e.Response;
}
}

Code running perfectly for http url but showing error:504 (gateway timeout) for https url

Code running fine for http URL but showing error when URL changes to HTTPS
ERROR: 504(GATEWAY_TIMEOUT)
List<TableauSite> SitesList = new List<TableauSite>();
string url = server + "/api/" + TableauServerVersion.version + "/sites";
HttpWebRequest wc = WebRequest.Create(url) as HttpWebRequest;
wc.Method = "GET";
wc.Headers.Add("x-tableau-auth", signedInObj.ACCESS_TOKEN);
try
{
HttpWebResponse wr = wc.GetResponse() as HttpWebResponse;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(wr.GetResponseStream());
SitesList = helperFunction(serverinfo, xmldoc, server,signedInObj);
return SitesList;
}
catch (WebException we)
{
//Catch failed request and return the response code
response = ((HttpWebResponse)we.Response).StatusCode.ToString();
ErrorHandling.getException(we);
}
return null;
}

Check if a.txt file exists or not on a remote webserver

I'm trying to check if .txt file is exists or not from web url. This is my code:
static public bool URLExists(string url)
{
bool result = false;
WebRequest webRequest = WebRequest.Create(url);
webRequest.Timeout = 1200; // miliseconds
webRequest.Method = "HEAD";
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)webRequest.GetResponse();
result = true;
}
catch (WebException webException)
{
//(url + " doesn't exist: " + webException.Message);
}
finally
{
if (response != null)
{
response.Close();
}
}
return result;
}
If i enter "http://www.example.com/demo.txt" is not a valid file path and website showing 404 error page then this code return true. How to solve this problem. Thanks in advance.
Use the StatusCode property of the HttpWebResponse object.
response = (HttpWebResponse)webRequest.GetResponse();
if(response.StatusCode == HttpStatusCode.NotFound)
{
result = false;
}
else
{
result = true;
}
Look through the list of possible status codes to see which ones you want to interpret as the file not existing.

Get detailed error information on creating httpwebrequest from c# to website

I am checking a website is active or not using below code. If website is inactive like screen shot. I want to log the detailed error information from the response. how can i do it..? please suggest..
private static bool CheckWebSiteActive()
{
bool isWebSiteActive = false;
try
{
StreamReader sr = null;
Stream resst = null;
HttpWebResponse response = null;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ConfigurationManager.AppSettings["webSiteToCheck"]);
request.Timeout = 10000;
request.AllowAutoRedirect = false; // find out if this site is up and don't follow a redirector
request.Method = "HEAD";
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException we)
{
using (WebResponse Wresponse = we.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)Wresponse;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream data = Wresponse.GetResponseStream())
{
string text = new StreamReader(data).ReadToEnd();
Console.WriteLine(text);
}
}
//response = (HttpWebResponse)we.Response;
//resst = response.GetResponseStream();
//sr = new StreamReader(resst);
log.Error(sr.ReadToEnd());
}
isWebSiteActive = ((int)response.StatusCode == 200 && response.StatusCode == HttpStatusCode.OK) ? true : false;
}catch(Exception Ex)
{
log.Error(Ex.Message.ToString());
}
return isWebSiteActive;
}
Thanks in adv.

C# How can I get server error from a URL?

We have a url and we need to check whether web page is active or not. We tried following code:
WebResponse objResponse = null;
WebRequest objRequest = HttpWebRequest.Create(URL);
objRequest.Method = "HEAD";
try
{
objResponse = objRequest.GetResponse();
objResponse.Close();
}
catch (Exception ex)
{
}
Above code gave exception if unable to get a response but also works fine even if we have a "server error" on that page? Any help how to get server error?
The HttpResponse class has a StatusCode property which you can check. If it's 200 everything is ok.
You can change your code to this:
HttpWebResponse objResponse = null;
var objRequest = HttpWebRequest.Create("http://google.com");
objResponse = (HttpWebResponse) objRequest.GetResponse();
if(objResponse.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("It failed");
}else{
Console.WriteLine("It worked");
}
For one thing, use a using statement on the response - that way you'll dispose of it whatever happens.
Now, if a WebException is thrown, you can catch that and look at WebException.Response to find out the status code and any data sent back:
WebRequest request = WebRequest.Create(URL);
request.Method = "HEAD";
try
{
using (WebResponse response = request.GetResponse())
{
// Use data for success case
}
}
catch (WebException ex)
{
HttpWebResponse errorResponse = (HttpWebResponse) ex.Response;
HttpStatusCode status = errorResponse.StatusCode;
// etc
}

Categories

Resources