1

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

6
  • 4
    How do you define "working"? Commented Jan 17, 2013 at 11:46
  • working means URL is accessible i mean its not down, its live. Commented Jan 17, 2013 at 11:49
  • where do you get "Request.InputStream"? You should be reading response stream from "response.GetResponseStream()" from the first code portion.. Commented Jan 17, 2013 at 11:49
  • Request.InputStream added in TestPage so it needs to read the request to get XML. i posted scenario why i have to do this way Commented Jan 17, 2013 at 11:52
  • Is it not simply a case of making the request and then checking that the StatusCode of the response is 200? Commented Jan 17, 2013 at 11:56

3 Answers 3

4

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;
Sign up to request clarification or add additional context in comments.

4 Comments

i have tested this code but as i told earlier this code has issue if the provided URL has some code in Page load it went on executing it. thats i dont want i just want to check headers to make it sure that URL is accessible
@user751959, sorry mis-understood the question, updating now.
if u have read my question i said i have used already webrequest.Method = "HEAD"; but yet it executes the PageLoad code of TestPage
4xx and 5xx errors throw an exception, so best to wrap this in a try{} catch{}.
1

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;
    }

}

Comments

1

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  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.