Http响应给出状态“Bad Request”Xamarin.Android

时间:2014-02-07 04:23:22

标签: c# android xamarin.android httpresponse http-status-code-400

我试图通过Http REST调用来调用Web服务。但我在回复中收到“错误请求”状态。我知道这意味着我的请求语法出了问题,但我无法找到它。

这是我的代码: -

public void GetToken ()
        {
            HttpWebRequest hwr =(HttpWebRequest) WebRequest.Create(string.Format("https://login.windows.net/{0}/oauth2/token", myApp.DomainName));
            hwr.Method = "POST";
            hwr.ContentType = "myApplication/x-www-form-urlencoded";
            hwr.BeginGetRequestStream(new AsyncCallback(SendTokenEndpointRequest), hwr);
        }

        protected void SendTokenEndpointRequest (IAsyncResult rez)
        {
            HttpWebRequest hwr = (HttpWebRequest)rez.AsyncState;   
            byte[] bodyBits = Encoding.UTF8.GetBytes(
                string.Format(
                    "grant_type=authorization_code&code={0}&client_id={1}&redirect_uri={2}",
                    myApp.Code, 
                    myApp.ClientId, 
                    HttpUtility.UrlEncode(myApp.RedirectUri)));
            Stream st = hwr.EndGetRequestStream(rez);
            st.Write(bodyBits, 0, bodyBits.Length);
            st.Close();
            hwr.BeginGetResponse(new AsyncCallback(RetrieveTokenEndpointResponse), hwr);
        }

        public void RetrieveTokenEndpointResponse (IAsyncResult rez)
        {
            HttpWebRequest hwr =(HttpWebRequest) rez.AsyncState;
            HttpWebResponse resp =(HttpWebResponse)hwr.EndGetResponse(rez); // ERROR HERE

            StreamReader sr = new StreamReader(resp.GetResponseStream());
            string responseString = sr.ReadToEnd();
            JObject jo = JsonConvert.DeserializeObject(responseString) as JObject;
            myApp.AccessToken = (string)jo["access_token"];


            System.Threading.Thread myThread = new  System.Threading.Thread (() => 
            {
                    ma.myWebView.GoBack ();
            });
            myThread.Start ();
        }

任何建议都表示赞赏。感谢。

0 个答案:

没有答案