繁体   English   中英

http请求c#授权

[英]http request c# authorization

我目前正在尝试通过带有此代码的授权通过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);
        }

一旦我进行了一些更改以添加一些身份验证,那么该代码无需身份验证就可以正常运行,而无需身份验证

这条线

WebResponse response2 = request2.GetResponse();

答复401-未经授权访问资源

我究竟做错了什么?

授权标头的示例是。

授权:基本QWxhZGRpbjpvcGVuIHNlc2FtZQ ==

我很好奇您的代币价值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM