【问题标题】:I am not getting response REST API (POST) Content Type FormUrlEncode我没有收到响应 REST API (POST) 内容类型 FormUrlEncode
【发布时间】:2018-09-17 11:12:30
【问题描述】:

当我尝试使用邮递员时,它工作正常并且我得到了响应。

我正在尝试提出请求。我没有得到任何回复,请查看我的代码并让我知道我哪里做错了?

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

输出:

【问题讨论】:

  • 提供的代码看起来正确。您将需要提供更多详细信息。包装该代码的方法是如何调用的?提供minimal reproducible example 可以帮助我们更好地了解您的实际问题。
  • 您是否调试过代码并检查您在response.StatusCode 中获得的值?看起来Debug.WriteLine 没有写入调试窗口。
  • 在 HttpResponseMessage 之后它没有进入“response.statuscode”行 @ChetanRanpariya 我在发送请求时没有收到任何响应,它自动开始到“GC”。
  • API 可能需要一些时间来响应。您是否在Debug.WriteLine( 行上设置了断点并等待它被击中?您的代码是否因任何异常而中断?如果有的话,也可以尝试在 Catch 块中设置断点。
  • 您的工作邮递员示例和您的代码示例使用不同的 URL...dexcom 与 userdomain

标签: c# xaml xamarin xamarin.forms httpclient


【解决方案1】:

对于这种类型的工作,我使用这个库: 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>();

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-18
    • 2015-09-02
    • 2017-10-31
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多