RestSharp response unauthorized

2020-04-12 09:06发布

I am new to web based solutions. I am hitting a rest url using RestSharp library.
My code is as follows:

var cleint = new RestClient("http://REST_URL");
cleint.Authenticator = new HttpBasicAuthenticator("username", "password");
var request = new RestRequest();
request.Method = Method.GET;
request.Resource = "0.json";

IRestResponse response = cleint.Execute(request);
if (response != null && ((response.StatusCode ==       HttpStatusCode.OK) &&
(response.ResponseStatus == ResponseStatus.Completed)))
  {
  // var arr = JsonConvert.DeserializeObject<JArray>    (response.Content);

  }

The url returns a json file, when I hit it manually. But I want to use a C# console application to get the json file and save it to the disk. I am getting an unauthorized response when I run the above mentioned code:
response.ResponseStatus= "Unauthorized"

1条回答
爷的心禁止访问
2楼-- · 2020-04-12 09:25

This is all it needed..

  client.Authenticator = new NtlmAuthenticator();

So if your IIS settings have Windows Authentication set as enabled, this is what you are going to need, Http Basic authentication is not enough to by pass the server security

查看更多
登录 后发表回答