使用基本身份验证连接到Restful Web服务

时间:2014-01-23 16:34:57

标签: c# rest

我正在尝试向RWS提出请求,并且我不断获得401 not authorized error

我已经提供了用于测试目的的示例用户名和密码,但我感觉我的实现已关闭。这是我第一次涉足RWS世界。

class Program
    {
        static string _address = "https://this.istheservice/";

        static async void RunClient()
        {
            HttpClientHandler handler = new HttpClientHandler();

            handler.UseDefaultCredentials = false;

            handler.Credentials = new NetworkCredential("Sample", "P@ssw0rd!!");

            HttpClient client = new HttpClient(handler);

            // Send asynchronous request
            HttpResponseMessage response = await client.GetAsync(_address);

            // Check that response was successful or throw exception
            response.EnsureSuccessStatusCode();//Breaks here

当我检查NetworkCredentials时,域名列为Domain = ""会导致问题吗?

如果我将他们提供的URL删除到浏览器中,我也会收到401错误。

如果需要更多信息,请询问,我不确定这里需要包含哪些活动部件。

我正在使用此示例:http://blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx我正在使用Fiddler来监控请求。

1 个答案:

答案 0 :(得分:0)

您尝试执行的操作不是基本身份验证。 要进行基本身份验证,您需要base64加密用户名:密码并将它们放在标题

   string basic = "Basic ENCRYPTEDSTRING";
   HttpClient client = new HttpClient();
   client.DefaultRequestHeaders.Add("Authorization", basic);

所以当你在fiddler

中看到它时,你的标题应该有这样的东西

授权:基本QWxhZGRpbjpvcGVuIHNlc2FtZQ ==

Wiki链接到基本身份验证http://en.wikipedia.org/wiki/Basic_access_authentication

相关问题