0

I am trying to get access a URL using .Net but when I run my Program I get the Error The remote server returned an error: (403) Forbidden. Now, the issue is if I click the link http://thisIsMyUR, and enter the user name and password as in the below code. It totally works. I am not able to understand why this exception is coming? Please refer the code below.

Side Note: I am using this sample function below to fire Build of my project in Jenkins Server.

string url = "http://thisIsMyURL";
WebRequest webRequest = WebRequest.Create(url);
webRequest.Credentials = new NetworkCredential("admin", "pass");
WebResponse response = webRequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();
4
  • Your code has some errors that would prevent it from even compiling. Commented Jan 3, 2018 at 16:18
  • fixed it, in the question. Commented Jan 3, 2018 at 16:20
  • Try setting Request.UseDefaultCredentials to false, and Request.PreAuthenticate to true before making the request. Commented Jan 3, 2018 at 16:23
  • @BradleyUffner Tried it, but does not work. Commented Jan 3, 2018 at 16:28

2 Answers 2

1

Other than what was recommended by Bradley Uffner in the comments, you can try to provide an actual user agent during the request. I have seen certain servers which would not respond to requests without that header for some odd perceived security reason.

EDIT: as requested, i'll update with some more information.

Some servers may choose to ignore requests, giving some error code (or closing connection) when certain conditions are not met. A good way of checking if that is the case, is to actually send all the standard headers sent by your average web browser in the request. The "User agent" is one of those headers, and this example adds it to your request:

string url = "http://thisIsMyURL";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.UserAgent = "USER AGENT VALUE";
HttpWebRequest.Credentials = new NetworkCredential("admin", "pass");
WebResponse response = webRequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseText = reader.ReadToEnd();

The cast to HttpWebRequest is required so you have access to the UserAgent header field. Other standard HTTP fields can be accessed this way.

You can check which headers are sent by your browser of choice by inspecting its requests using the browser developer tools (usually Right click on webpage -> Inspect elements -> Network).

Sign up to request clarification or add additional context in comments.

5 Comments

Could you please elaborate your answer.
I added some extra information which may clarify what I talked about.
Still does not work, i used Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Win64; x64; Trident/4.0) as useragent string
You can try using other parameters. It is a possibility it's requesting any of them.
It is so ridiculous, I tried with powershell and it works amazingly, I even gave Admin rights to the executable. I can not understand.
0

I figured it out with the help of some articles. A basic authentication needs to be added. Below is a sample.

 var userName = "admin;"
 var password = "password";
 var encodedAuthentication = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));
 webRequest.Headers.Add("Authorization", "Basic " + encodedAuthentication);

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.