【问题标题】:HTTP PostAsync returns nothingHTTP PostAsync 不返回任何内容
【发布时间】:2021-01-26 21:09:12
【问题描述】:

下面的代码适用于向 Webhook.site 发送 HTTP 帖子,但是当对我自己的 azurewebsite 执行相同的请求时,调试器会在 postasync 处停止,并且“response”变量保持为空。

我的天蓝色网站从 ReqBin 的 json-string POST 返回 200。我的 excel 应用程序可以使用下面的代码将工作 http 帖子发送到 Webhook.site,但不能发送到我自己的 azurewebsite。我错过了什么?

一些资源表明 SSL 验证可能会导致问题?不知道是不是这样。

private static readonly HttpClient client = new HttpClient();

public async Task<HttpResponseMessage> PostRequest(IRibbonControl control)
{
    var content = new StringContent(json_object.ToString(), System.Text.Encoding.UTF8, "application/json");

    //This is where i input my own website and it doesn't work
    HttpResponseMessage response = await client.PostAsync("https://webhook.site/9b994ad0-81a1-496f-b910-d48d0567b1b8", content).ConfigureAwait(false); 

    var responseString = await response.Content.ReadAsStringAsync();

    return response;
}

感谢您的帮助。

【问题讨论】:

  • 您可能希望投资于应用程序日志记录,以便在部署的代码中查看异常和其他调试信息。
  • “什么都不返回”是什么意思?内容为空?它返回哪个http状态码?
  • @Leff deugger 只是停在 postasync 行上,“response”变量保持不变(null)。
  • “返回 null”与“不返回任何内容”完全不同。请将该信息编辑到您的问题中。
  • 是的@Ian Kemp,我编辑了我的问题。

标签: c# http asynchronous


【解决方案1】:

要在调试中查看 postAsync 方法的结果,请执行 2 个步骤。截图:postAsync debug

  1. 从带有 postAsync 的方法返回 HttpResponseMessage

    private static async Task<HttpResponseMessage> methodWithPostAsync(){
    ...
    response = await client.PostAsync(url, data);
    return response
    }
    
  2. 调用方法和等待响应消息状态

    Task<HttpResponseMessage> content= methodWithPostAsync();
    while (!content.IsCompleted)
    {
        Console.WriteLine("busy");
        System.Threading.Thread.Sleep(1000);
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2019-04-06
    • 2012-02-15
    • 2020-09-07
    • 2021-11-22
    • 2015-11-06
    • 2013-11-14
    相关资源
    最近更新 更多