我没有收到响应REST API(POST)内容类型FormUrlEncode

时间:2018-09-17 11:12:30

标签: c# xaml xamarin xamarin.forms httpclient

当我尝试邮递员时,它工作正常,并且得到了答复。

enter image description here

我正在尝试提出请求。我没有得到任何回应,请查看我的代码,让我知道我做错了什么地方?

  using (var client = new HttpClient())
            {

                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("client_id", "11223asda"),
                    new KeyValuePair<string, string>("client_secret", "1232asdasa"),
                    new KeyValuePair<string, string>("code", "authcode3"),
                    new KeyValuePair<string, string>("grant_type", "authorization_code"),
                    new KeyValuePair<string, string>("redirect_uri", "http://www.google.com/")

                });

                var uri = new Uri("https://sandbox-api.userdomain.com/v2/oauth2/token");


                // var content = new FormUrlEncodedContent(obj);

                //HttpResponseMessage response = null;

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);

                request.Content = content;

                HttpResponseMessage response = await client.SendAsync(request);

                Debug.WriteLine(response.StatusCode);

输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

对于这种类型的工作,我使用以下lib: https://github.com/jgiacomini/Tiny.RestClient

var client = new TinyRestClient(new HttpClient(),"https://sandbox-api.userdomain.com/");

var response = await client.
                PostRequest("v2/oauth2/token").
                AddFormParameter("client_id", "France").
                AddFormParameter("client_secret", "Paris").
                AddFormParameter("grant_type", "Paris").
                AddFormParameter("redirect_uri", "Paris").
                ExecuteAsync<Response>();

希望有帮助!