You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 4.6中HttpClient.PostAsJsonAsync调用无响应问题求助

问题:调试时PostAsJsonAsync执行后程序直接退出无响应无异常

调试模式下运行项目,执行到PingServerAsync方法中的以下代码时:

var response = await httpClient.PostAsJsonAsync(url, payload);

程序短暂卡顿后直接退出,既不返回响应值,也未触发异常,相关日志也未记录响应信息。

相关代码

CheckDicomServersAsync方法

public async Task CheckDicomServersAsync(string hostName)
{
     ...
     var cstore = result.FirstOrDefault();
     MqttService.applog.Debug($"****** Gonna Dicom Echo this - {cstore.ClientAETitle} *******");
    string resp = await PingDicomServerAsync(cstore);
    Console.WriteLine($"Dicom Echo response: {resp}");
}

PingServerAsync方法

private async Task<string> PingServerAsync(DICOMClient client)
{
    EchoViewModelSettings payload = new EchoViewModelSettings();
    payload.ClientAE = client.ClientAETitle;
    // 其他属性赋值...           
    
    using (httpClient)
    {
        httpClient.BaseAddress = new Uri(m_appConfig.DICOM_URL);
       
        var requestUri = handler + "/Echo?SessionID=" + System.Web.HttpUtility.UrlEncode(session.SessionID.ToString());

        try
        {
            MqttService.applog.Debug($"Make request: {requestUri}");

            var url = "http://localhost/MyDicomAPI/api/dicom/Echo?SessionID=my_session";

            // 发送POST请求
            var response = await httpClient.PostAsJsonAsync(url, payload);                    

            MqttService.applog.Debug($"The Response: {response.Content}");

            if (response.IsSuccessStatusCode)
            {
                var echoResponse = await response.Content.ReadAsStringAsync();
                MqttService.applog.Debug($"The Echo Response is: {echoResponse}");
            }                    
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine(response.StatusCode.ToString());
                return response.StatusCode.ToString();
            }
            else
            {
                return response.StatusCode.ToString();
            }
        }
        catch (Exception ex)
        {
            MqttService.applog.Error($"WTF is wrong now: {ex}");
            throw;
        }
    }
}

Postman测试验证

使用Postman调用相同的URL和JSON请求体,能够正常返回响应(值为0表示成功):
Postman正常响应截图

更新:补充调用链(临时绕过Windows服务)

Program.cs代码

// Program.cs
static void Main()
{
    // 注释掉Windows服务启动代码
    //ServiceBase[] ServicesToRun;
    //ServicesToRun = new ServiceBase[]
    //{
    //    new MqttService()
    //};
    //ServiceBase.Run(ServicesToRun);

    MqttService mqttSvc = new MqttService(); // 调试版本
    
    mqttSvc.StartDebugMode();
}

MqttService.cs代码

// MqttService.cs
public void StartDebugMode()
{
    Init();

    this.CheckOtherServers(hostName);

    // 注释掉其他服务启动代码
    //StartSubscriber();
    //StartPublisher();
    //PublishHeartBeat(null, null);            
}

private async void CheckOtherServers(string hostName)
{
    if (this.pingDicom)
    {
        DicomStatus dicom = new DicomStatus();
        await dicom.CheckDicomServersAsync(hostName);
    }
}

内容的提问来源于stack exchange,提问作者bob.mazzo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 18:25:01