【发布时间】: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