locked
How to Provide authorization to a web request if the first request in redirecting to the other URL RRS feed

  • Question

  • Hello Everyone,

    I have to call a web api in my project, for that I have to send a request to the WebAPI on a URL which in return gives me a response in JSON. But, the problem is, when the request is sent to the first URL with authorization, it redirects the request to another URL, where Authorization fails and I get an Error 401.

    Here is my piece of code

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlString);

                httpWebRequest.ContentType = "application/json";

                httpWebRequest.Method = "GET";

                string result; //result is the String in which Json is Stored

                string api_idtoken_id = api_id+"+" + tokenid;

                httpWebRequest.Headers.Add("Authorization", "Bearer " + api_idtoken_id);

                AuthenticationManager.Unregister("Basic");

                 httpWebRequest.AllowAutoRedirect = true;
          

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();  <-- At this point always Error is shown (401)
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                    var jo = JObject.Parse(result);

                }

    Please tell me is there any way to provide authorization.

    • Moved by Kristin Xie Thursday, October 15, 2015 8:50 AM
    Wednesday, October 14, 2015 2:40 PM

Answers