C# How to Check If a Url Exists/Is Valid

C# How can I check if a URL exists/is valid?

You could issue a "HEAD" request rather than a "GET"?
So to test a URL without the cost of downloading the content:

// using MyClient from linked post
using(var client = new MyClient()) {
client.HeadOnly = true;
// fine, no content downloaded
string s1 = client.DownloadString("http://google.com");
// throws 404
string s2 = client.DownloadString("http://google.com/silly");
}

You would try/catch around the DownloadString to check for errors; no error? It exists...


With C# 2.0 (VS2005):

private bool headOnly;
public bool HeadOnly {
get {return headOnly;}
set {headOnly = value;}
}

and

using(WebClient client = new MyClient())
{
// code as before
}

How to check whether the URL is valid or not

Try like below, It will help you....

     protected void btnRender_Click(object sender, EventArgs e)
{
if(CheckUrlExists(urltxt.Text))
{
string strResult = string.Empty;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(urltxt.Text);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
sr.Close();
}
strResult = strResult.Replace("<form id='form1' method='post' action=''>", "");
strResult = strResult.Replace("</form>", "");
TextBox1.Text = strResult.Trim();
div.InnerHtml = strResult.Trim();
}
else
{
MessageBox.Show("Not a Valid URL");
}
}

Checking if URL exists - HTTP Request always returns an exception

Check the status WebException.Status
This will let you know what specific web exception has occured.

Update: Try change the request.Method = "HEAD";
to GET and try.

Try with a unavailable (404) url, compare the status. Check whether anything is blocking your request.

This is how i manage in my code, i am handling using only ftp specific status.'CommStatus' is an ENUM with error codes which is available in whole application.

catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
switch(response.StatusCode)
{
case FtpStatusCode.ActionNotTakenFileUnavailable:
return CommStatus.PathNotFound;
case FtpStatusCode.NotLoggedIn:
return CommStatus.AuthenticationError;
default: return CommStatus.UnhandledException;

}


}

Below are the available Status of WebException.

CacheEntryNotFound

This API supports the product infrastructure and is not intended to be used directly from your code. The specified cache entry was not found.

ConnectFailure

This API supports the product infrastructure and is not intended to be used directly from your code. The remote service point could not be contacted at the transport level.

ConnectionClosed

This API supports the product infrastructure and is not intended to be used directly from your code. The connection was prematurely closed.

KeepAliveFailure

This API supports the product infrastructure and is not intended to be used directly from your code. The connection for a request that specifies the Keep-alive header was closed unexpectedly.

MessageLengthLimitExceeded

This API supports the product infrastructure and is not intended to be used directly from your code. A message was received that exceeded the specified limit when sending a request or receiving a response from the server.

NameResolutionFailure

This API supports the product infrastructure and is not intended to be used directly from your code. The name resolver service could not resolve the host name.

Pending
This API supports the product infrastructure and is not intended to be used directly from your code. An internal asynchronous request is pending.

PipelineFailure
This API supports the product infrastructure and is not intended to be used directly from your code. The request was a piplined request and the connection was closed before the response was received.

ProtocolError

This API supports the product infrastructure and is not intended to be used directly from your code. The response received from the server was complete but indicated a protocol-level error. For example, an HTTP protocol error such as 401 Access Denied would use this status.

ProxyNameResolutionFailure

This API supports the product infrastructure and is not intended to be used directly from your code. The name resolver service could not resolve the proxy host name.

ReceiveFailure

This API supports the product infrastructure and is not intended to be used directly from your code. A complete response was not received from the remote server.

RequestCanceled
This API supports the product infrastructure and is not intended to be used directly from your code. The request was canceled, the WebRequest.Abort method was called, or an unclassifiable error occurred. This is the default value for Status.

RequestProhibitedByCachePolicy

This API supports the product infrastructure and is not intended to be used directly from your code. The request was not permitted by the cache policy. In general, this occurs when a request is not cacheable and the effective policy prohibits sending the request to the server. You might receive this status if a request method implies the presence of a request body, a request method requires direct interaction with the server, or a request contains a conditional header.

RequestProhibitedByProxy

This API supports the product infrastructure and is not intended to be used directly from your code. This request was not permitted by the proxy.

SecureChannelFailure

This API supports the product infrastructure and is not intended to be used directly from your code. An error occurred while establishing a connection using SSL.

SendFailure
This API supports the product infrastructure and is not intended to be used directly from your code. A complete request could not be sent to the remote server.

ServerProtocolViolation
This API supports the product infrastructure and is not intended to be used directly from your code. The server response was not a valid HTTP response.

Success
This API supports the product infrastructure and is not intended to be used directly from your code. No error was encountered.

Timeout
This API supports the product infrastructure and is not intended to be used directly from your code. No response was received during the time-out period for a request.

TrustFailure

This API supports the product infrastructure and is not intended to be used directly from your code. A server certificate could not be validated.

UnknownError

This API supports the product infrastructure and is not intended to be used directly from your code. An exception of unknown type has occurred.

More details here: https://msdn.microsoft.com/en-us/library/system.net.webexceptionstatus(v=vs.110).aspx

How to check if website exists

You need to use the GetResponseAsync()

HttpWebResponse response = await webrequest.GetResponseAsync();

can I check if a file exists at a URL?

If you're attempting to verify the existence of a web resource, I would recommend using the HttpWebRequest class. This will allow you to send a HEAD request to the URL in question. Only the response headers will be returned, even if the resource exists.

var url = "http://www.domain.com/image.png";
HttpWebResponse response = null;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";


try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
/* A WebException will be thrown if the status of the response is not `200 OK` */
}
finally
{
// Don't forget to close your response.
if (response != null)
{
response.Close();
}
}

Of course, if you want to download the resource if it exists it would most likely be more efficient to send a GET request instead (by not setting the Method property to "HEAD", or by using the WebClient class).

Use httpwebrequest to check if url exists

I finally got to the point of bieng able to validate all the urls without exception.

Firstly I took Davios advice. Some domains return an error on Request.HEAD so I have included a retry for specific scenarios. This created a new Request.GET for the second request.

Secondly, the Amazon scenario. Amazon was intermittently returning a 503 error for its own site and permanent 503 errors for sites hosted on the Amazon framework.

After some digging I found adding the following line to the Request resolved both. It is the Accept string used by Firefox.

var request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

How to check if a url is valid or not

You seem to be confused about what exactly URL (or URI, the difference is not significant here) is. For example, http://stackoverflow.com is a valid absolute URL. On the other hand, stackoverflow.com is technically a valid relative URL, but it would refer to the file named stackoverflow.com in the current directory, not the website with that name. But stackoverflow.com is a registered domain name.

If you want to check whether a domain name is valid, you need to define what exactly do you mean by “valid”:

  1. Is it a valid domain name? Check whether the string consists of parts separated by dots, each part can contain letters, numbers and a hyphen (-). For example, asas and this.not.valid are both valid domain names.
  2. Could it be an Internet domain name? Domain names on the Internet (as opposed to intranet) are specific in that they always have a TLD (top-level domain). So, asas certainly isn't an Internet domain name, but this.not.valid could be.
  3. Is it a domain name under existing TLD? You can download the list of all TLDs and check against that. For example, this.not.valid wouldn't be considered valid under this rule, but thisisnotvalid.com would.
  4. Is it a registered domain name?
  5. Does the domain name resolve to an IP address? A domain name could be registered, but it still may not have an IP address in its DNS record.
  6. Does the computer the domain name points to respond to requests? The requests that make the most sense are a simple HTTP request (e.g. trying to access http://domaininquestion/) or ping.


Related Topics



Leave a reply



Submit