简体   繁体   English

HttpWebResponse 401-error未经授权

[英]HttpWebResponse 401-error Unauthorized

I'm currently having issues with a HttpWebRequest/Response when I'm trying to make calls to an API. 当我尝试调用API时,我目前遇到HttpWebRequest / Response问题。

The API use JSON/Ajax to make calls, but I need to make the calls through HttpWebRequest/Response which I don't fully understand and I think I'm passing the wrong access token. API使用JSON / Ajax进行调用,但我需要通过HttpWebRequest / Response进行调用,我不完全理解,我认为我传递了错误的访问令牌。

Here's my current code, which I got from a tutorial: 这是我目前的代码,我从教程中得到的代码:

        byte[] buffer = Encoding.ASCII.GetBytes("username=User&password=Password");

        HttpWebRequest WebReq =(HttpWebRequest)WebRequest.Create("http://api40.maildirect.se/User/Authorize");

        WebReq.Method = "POST";
        WebReq.ContentType = "application/x-www-form-urlencoded";
        WebReq.ContentLength = buffer.Length;

        Stream PostData = WebReq.GetRequestStream();

        PostData.Write(buffer, 0, buffer.Length);
        PostData.Close();

        HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

        var status = WebResp.StatusCode; (returns OK)

        Stream _Answer = WebResp.GetResponseStream();
        StreamReader Answer = new StreamReader(_Answer);

        //Here I try to make another request, do I need to make another instance?
        WebReq = (HttpWebRequest)WebRequest.Create("http://api40.maildirect.se/Contacts?&select=ContactId,FirstName,LastName");
        WebReq.Method = "GET";
        WebReq.ContentType = "text/json; charset=utf-8";
        WebReq.Headers.Add("Authorization", Answer.ReadToEnd());

        HttpWebResponse WebResp2 = (HttpWebResponse)WebReq.GetResponse();

         ^ This is where it breaks because I'm not authorized.

And the only useful documentation from the API is that I need to pass the access token when making additional calls, but I suspect I'm passing the wrong token. API中唯一有用的文档是我需要在进行其他调用时传递访问令牌,但我怀疑我传递了错误的令牌。

Thanks in advance 提前致谢

deSex deSex

I was sending the HttpWebResponse as the token, instead of the string that resulted from the StreamReader. 我发送了HttpWebResponse作为令牌,而不是StreamReader产生的字符串。 I created a new variable called token with this string value and added it to the header "Authorization" as should be. 我用这个字符串值创建了一个名为token的新变量,并将其添加到标题“Authorization”中。

        byte[] buffer = Encoding.ASCII.GetBytes("username=Username&password=Password");
        HttpWebRequest WebReq = (HttpWebRequest) WebRequest.Create("http://api40.maildirect.se/User/Authorize");

        WebReq.Method = "POST";
        WebReq.ContentType = "application/x-www-form-urlencoded";
        WebReq.ContentLength = buffer.Length;

        Stream PostData = WebReq.GetRequestStream();

        PostData.Write(buffer, 0, buffer.Length);
        PostData.Close();

        HttpWebResponse WebResp = (HttpWebResponse) WebReq.GetResponse();

        //This token variable was something I missed creating and it also had to get the " removed to work properly.
        var token = new StreamReader(WebResp.GetResponseStream()).ReadToEnd().Replace("\"", "");

        WebReq = (HttpWebRequest) WebRequest.Create("http://api40.maildirect.se/Contacts");

        WebReq.Method = "GET";
        WebReq.ContentType = "application/json; charset=utf-8";

        // This is where another part of the problem was, sent in the wrong type to the header, but when I replaced it with the token variable it worked like a charm :)
        //WebReq.Headers.Add("Authorization", WebResp.ToString());

        WebReq.Headers.Add("Authorization", token);

        HttpWebResponse WebResp2 = (HttpWebResponse) WebReq.GetResponse();

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

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