简体   繁体   中英

HttpWebResponse 401-error Unauthorized

I'm currently having issues with a HttpWebRequest/Response when I'm trying to make calls to an API.

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.

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.

Thanks in advance

deSex

I was sending the HttpWebResponse as the token, instead of the string that resulted from the StreamReader. I created a new variable called token with this string value and added it to the header "Authorization" as should be.

        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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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