简体   繁体   中英

Check URL exists using c#

i have been trying to find out either provide URL is available or not. Available doesnt mean domain availability i mean either URL is accessible or its not accessible

i have tested code

var webrequest = (HttpWebRequest)WebRequest.Create(
                                       "http://localhost:64519/TestPage.aspx");
webrequest.Method = "HEAD";
HttpWebResponse response = webrequest.GetResponse() as HttpWebResponse;

and there is some code on pageload of Testpage

protected void Page_Load(object sender, EventArgs e)
{
    StreamReader stream = new StreamReader(Request.InputStream);
    XDocument xmlInput = XDocument.Load(stream);
}

now issue is even i added HEAD in request yet it goes in to PageLoad and throws exception.

Scenario: i have been trying to send XML to provided URL. in XML case its working fine but when i try to check that either Link is live or not it throws exception because XDocument.Load(stream); dont have XML\\ surely i can solve the issue by using

if (stream.BaseStream.Length != 0)
{
    XDocument xmlInput = XDocument.Load(stream); 
}

but its not appropriate. i just want to know the link is live or not based on my research is just Add headers but even with adding headers my problem is yet there

so please some one can help me out with this or any kind of help will be appreciated

You could use the HttpWebRequest and HttpWebResponse classes and set the request's Method to "HEAD".

List of other possible Methods.

var request = (HttpWebRequest)WebRequest.Create("http://localhost:64519/TestPage.aspx");
request.Method = "HEAD";

var response = (HttpWebResponse)request.GetResponse();

var success = response.StatusCode == HttpStatusCode.OK;

I've made a function on the fly. Hope that it works for you :)

public bool isValid(string url) {
    Stream sStream;
    HttpWebRequest urlReq;
    HttpWebResponse urlRes;

    try {
        urlReq = (HttpWebRequest) WebRequest.Create(url);
        urlRes = (HttpWebResponse) urlReq.GetResponse();
        sStream = urlRes.GetResponseStream();

        string read = new StreamReader(sStream).ReadToEnd();
        return true;

    } catch (Exception ex) {
        //Url not valid
        return false;
    }

}

Use the GET method

If the Website Respond your Query then Get the Response Data...

If there is no such URL then it throws the WebException Error... Yoiu Can catch that and do something on that...

Here i list my idea. I think it solve ur problem

try
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:64519/TestPage.aspx");
    webRequest.Method = "GET";

    string responseData = string.Empty;
    HttpWebResponse httpResponse = (HttpWebResponse)webRequest.GetResponse();

    using (StreamReader responseReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        responseData = responseReader.ReadToEnd();
    }
}
catch (System.Net.WebException ex)
{
   //Code - If does not Exist  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM