【问题标题】:http request c# authorizationhttp请求c#授权
【发布时间】:2014-04-29 15:20:15
【问题描述】:

我目前正在尝试使用此代码通过 https 发布请求

const string url = "http://api.xxx.pt/";
        const string username = "username";
        const string password = "password";
        const string token = "token";
        const string json = "{jsondata}";

        try
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request2 = WebRequest.Create(url);

            request2.Headers["Authorization"] = token;

            // Set the Method property of the request to POST.
            request2.Method = "POST";

            // Create POST data and convert it to a byte array.
            string postData = json;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Set the ContentType property of the WebRequest.
            request2.ContentType = "application/json";

            // Set the ContentLength property of the WebRequest.
            request2.ContentLength = byteArray.Length;

            // Get the request stream.
            Stream dataStream = request2.GetRequestStream();

            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);

            // Close the Stream object.
            dataStream.Close();


            // Get the response.
            WebResponse response2 = request2.GetResponse();

            // Display the status.
            Console.WriteLine(((HttpWebResponse)response2).StatusDescription);

            // Get the stream containing content returned by the server.
            dataStream = response2.GetResponseStream();

            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);

            // Read the content.
            string responseFromServer = reader.ReadToEnd();

            // Display the content.
            Console.WriteLine(responseFromServer);

            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response2.Close();
        }
        catch (WebException e)
        {
            Console.WriteLine("This program is expected to throw WebException on successful run." +
                                "\n\nException Message :" + e.Message);
            if (e.Status == WebExceptionStatus.ProtocolError)
            {
                Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

一旦来自 MSDN 库,此代码无需身份验证即可正常工作 一旦我进行了一些更改以添加一些身份验证

这一行

WebResponse response2 = request2.GetResponse();

回复 401 - 未授权访问资源

我做错了什么?

【问题讨论】:

  • 为了使身份验证顺利进行,我将其发送到 API GET "api url" -H "Authorization: Token token=example_token"
  • 这是正确的方法吗?还是 webclient 会更容易?
  • 有人能给出一个 C# 代码行的例子吗?

标签: c# json http-headers httprequest


【解决方案1】:

授权头的一个例子是。

授权:基本 QWxhZGRpbjpvcGVuIHNlc2FtZQ==

我很想知道你的代币价值。

【讨论】:

  • 是这样的授权:基本ooshei2eeloh6eid7Ieb
  • 您可以使用 google chrome developer 之类的工具捕获您的帖子有效负载吗?请检查您的授权标头,看看它是否存在于帖子标头中。
  • 请尝试这个添加授权头 theRequest.Headers.Add("Authorization", authToken);
  • thanx 但它仍然抛出异常 =/
  • 是否添加了授权标头?您的网址与您的网站不同吗?服务 url 允许 CORS 调用吗?
猜你喜欢
  • 2014-04-19
  • 2021-04-04
  • 2015-03-08
  • 2021-04-26
  • 2021-05-12
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多