【问题标题】:C# how to get the JSON string by using HttpWebRequestC#如何使用HttpWebRequest获取JSON字符串
【发布时间】:2017-05-27 05:30:30
【问题描述】:

任何人都可以帮助我从 gitlab 站点获取 JSON。我已经编写了代码,但是当我编译代码时,我在控制台应用程序 (C#) 中收到异常 The remote server returned an error: (401) Unauthorized.

在我的浏览器中,我在登录浏览器时运行相同的 URL,我可以获得 JSON 字符串。如果我在退出浏览器后运行相同的 URL,我会在浏览器中收到 {"message":"401 Unauthorized"} 消息。

由于同样的异常,我认为我没有将用户名和凭据传递到我的HttpWebRequest

我在HttpWebResponse response = request.GetResponse() as HttpWebResponse; 行中遇到异常

我的代码:

            string URL = "http://gitlab.company.com/api/v3/users?per_page=100&page=1";  
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.ContentType = "application/json; charset=utf-8";
            request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("userName:passWord"));
            request.PreAuthenticate = true;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                Console.WriteLine(reader.ReadToEnd());
            }

我的问题:

  1. 我在哪里犯了什么错误?
  2. 我知道我必须使用 GET 动词来获取 API 中的数据,但我不知道我必须在哪里使用 GET 动词?

提前致谢。

【问题讨论】:

  • 我的意思是,当您使用“request.Headers”时,无需设置“request.PreAuthenticate = true;”。至少它在我的代码中是如何工作的(不是用于 gitlab,而是用于另一台服务器)。 (但我使用“request.Headers.Add”)。你的代码应该包含 "request.Method = "GET";"
  • 从实际阅读错误信息开始:​​未经授权。这意味着您使用了错误的凭据。你为什么不使用Credentials 属性,如下所示How to use HttpWebRequest.Credentials Property for Basic Authentication?
  • 另外,您为什么使用 HttpWebRequest 而不是 HttpClient?所有这些行都可以减少到 3-4
  • @Julo 我在我的代码中添加了 request.method 并运行它。但现在我也面临同样的错误。
  • @Arunald 不幸的是,我这周才开始进行 Web 请求测试(针对特定项目)。在我的回复中,我只写了在您的代码和适用于我的代码之间发现的差异(在不同的服务器上)。该页面有一个使用基本方法的授权 API (/auth),我通过 JSON 数据发布(命令)我的用户数据(名称/密码)。作为响应,我得到一个令牌,然后将此令牌用作其他 API 函数的授权令牌。

标签: c# json httpwebrequest getjson gitlab-api


【解决方案1】:

在您的请求中使用编码,凭据需要以 ISO-8859-1 编码

        string URL = "http://gitlab.company.com/api/v3/users?per_page=100&page=1";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.ContentType = "application/json; charset=utf-8";
        request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes("username:password"));
        request.PreAuthenticate = true;
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        using (Stream responseStream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            Console.WriteLine(reader.ReadToEnd());
        }

如果这不起作用,请删除 Headers 行并将其替换为以下内容:

request.Credentials = new NetworkCredential("username", "password");

【讨论】:

  • 我在我的代码中尝试了两种更改。但我对这两项更改都面临同样的例外(401 未经授权)
  • 我建议你联系syncfusion并询问他们访问页面需要什么样的身份验证。似乎他们不使用基本的身份验证方法。
猜你喜欢
  • 1970-01-01
  • 2017-02-22
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 2017-04-27
相关资源
最近更新 更多