0

I need to read status (403 or 404) of HTTP-header when read a URL by using C# in ASP.Net. For example: URL1: https://www.instagram.com/khaniki.ah URL2: https://www.instagram.com/khaniki.ah123456 I get status 200 (HttpStatusCode.Found) when I try to read URL1 and same for reading URL2 while it's not exist; See more in photos.

Also, I use this following code:

    var Url = UrlTbx.Text;
    Uri urlCheck = new Uri(Url);

    HttpWebRequest HttpRequest = (HttpWebRequest)WebRequest.Create(Url);
    HttpRequest.Timeout = 15000;
    HttpRequest.AllowAutoRedirect = false;
    HttpRequest.ServicePoint.Expect100Continue = false;
    HttpRequest.ContentType = "application/x-www-form-urlencoded";
    //HttpRequest.Method = "GET";
    HttpRequest.Method = "HEAD";//both methods was tested
    HttpWebResponse HttpResponse;

    try
    {
        HttpResponse = (HttpWebResponse)HttpRequest.GetResponse();

        if (HttpResponse.StatusCode == HttpStatusCode.Found)
            Lit.Text = "YES" + " / " + HttpResponse.StatusCode;
        else if (HttpResponse.StatusCode == HttpStatusCode.NotFound)
            Lit.Text = "NO" + " / " + HttpResponse.StatusCode;
    }
    catch (Exception)
    {
       return false;
    }

URL1 URL2

1 Answer 1

2

The server you're requesting doesn't seem to accept HEAD requests. Try to keep the GET header method and try to remove

HttpRequest.ContentType = "application/x-www-form-urlencoded";

Also Found is not 200. You're talking about HttpStatusCode.OK for 200. I get the 404 you're looking for when doing simply

   var Url = "https://www.instagram.com/khaniki.ah123456/";
   Uri urlCheck = new Uri(Url);

   HttpWebRequest HttpRequest = (HttpWebRequest)WebRequest.Create(Url);

   HttpRequest.Method = "Get";

   var dHttpWebResponse = await HttpRequest.GetResponseAsync();

or try with the httpclient api, (to avoid the exception)

var Url = "https://www.instagram.com/khaniki.ah123456/";
        Uri urlCheck = new Uri(Url);
        var client = new HttpClient();
        var res = client.GetAsync(urlCheck).Result;
        var statusCode = res.StatusCode; //should be 404
Sign up to request clarification or add additional context in comments.

6 Comments

I also used Get method
I tested your opinion but I cannot find or get status 400 in header. See response "Pragma : no-cache Vary : Accept-Language Content-Language : en Strict-Transport-Security : max-age=86400 Connection : keep-alive Content-Length : 0 Cache-Control : private, no-cache, no-store, must-revalidate Content-Type : text/html; charset=utf-8 Date : Thu, 27 Jul 2017 10:58:20 GMT Expires : Sat, 01 Jan 2000 00:00:00 GMT Location : instagram.com/khaniki.ah123456"
I updated my answer, with httpclient class you should have a 404 instead of an exception
It works and tank you! Is there any other solution or suggestion that return code?
I don"t understand , what do you want ? another solution ? if you want to stick with the WebRequest one, you have to catch the WebException and read the status code inside it... if it's ok then you can mark this as answered by checking it.
|

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.