Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys,

I am trying to get status code of URL. I'm using the below code to achieve it. It is working good. But problem is, The code is showing some valid URL's also not working.

Please see the code below where the URL perfectly working on Browser but code returning the below results:

The remote server returned an error: (403) Forbidden

Why? need your help to fix it.
Thanks in advance.

What I have tried:

I tried with different codes but all are not giving the accurate results.
try
{
    string url="https://www.digikey.com/product-detail/en/rushup/CLOUD-JAM-L4/1770-1003-ND/7388464";
    // Creates an HttpWebRequest for the specified URL. 
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    // Sends the HttpWebRequest and waits for a response.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
        Console.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}",
            myHttpWebResponse.StatusDescription);
    // Releases the resources of the response.
    myHttpWebResponse.Close();
}
catch (WebException e)
{
    Console.WriteLine("\r\nWebException Raised. The following error occured : {0}", e.Status);
}
catch (Exception e)
{
    Console.WriteLine("\nThe following Exception was raised : {0}", e.Message);
} 
Posted
Updated 16-Nov-18 3:05am
v3
Comments
F-ES Sitecore 16-Nov-18 5:58am    
Look at the request when you do it in the browser (use the network tab in the browser tools to do this) and compare that with your code version. The browser will probably be sending some kind of authentication data to validate the user that your code version isn't.

Try using this:
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.UseDefaultCredentials = true;
myHttpWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
myHttpWebRequest.UserAgent = "MyHttpRequester";

This will let the application use the credentials of the logged in user to access the site.
 
Share this answer
 
v2
Comments
Member 11570261 16-Nov-18 7:00am    
I'm still facing the same issue. I'm running this code in C#-console app.
Help me to resolve the issue.
AndriiMakhota 16-Nov-18 7:14am    
Adding my 2 strings of code after
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);

not helps?
Member 11570261 16-Nov-18 7:21am    
Thanks for the response.
Yes, still returning 403 status code. But I'm expecting 200 status code.
AndriiMakhota 16-Nov-18 8:27am    
Strange, it's working on my comp. Try to add data about client, something like:
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.UserAgent = "MyHttpRequester";
Member 11570261 19-Nov-18 8:50am    
Thank you.
Now it is working and returning the status code 200.
How the UserAgent is helping to resolve the issue?
myHttpWebRequest.UserAgent = "MyHttpRequester";

I'm going to use to host this code on Azure function app. So what UserAgent value I have to place. Can I use the below code on Azure function app?
myHttpWebRequest.UserAgent = "MyHttpRequester";
OR

myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763";

Please share your Suggestion.
I think your problem is caused by double quotes in the url string, do not use:
string url="<a href="https://www.digikey.com"";
But use:
string url = @"<a href=""https://www.digikey.com""";
 
Share this answer
 
Comments
Member 11570261 16-Nov-18 8:31am    
Thanks for the response.
It is a console based application and there no chance of double quotes issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900