简体   繁体   English

Post Rest Web API在Postman上工作,但未从C#项目中的代码返回任何响应

[英]Post rest web api working from Postman but not returning any response from code in C# project

There is a POST rest api which used to work from code before. 有一个POST rest api,以前可以从代码开始工作。 But recently it is broken and is not returning any response. 但最近它已损坏,没有返回任何响应。 However if I try to call the api from the Postman, then it works fine. 但是,如果我尝试从Postman调用api,则可以正常工作。

In what way can I debug this to find the root cause of the issue ? 我可以通过什么方式调试它以找到问题的根本原因?

Following is the C# code which I am using to call this post rest api 以下是我用来调用此帖子休息api的C#代码

 public async Task SaveToServerAsync()
    {
        string filePath = @"<filePath>";
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        // tried this line of code from another SO answer, but this didn't work either   
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("https://<server name>/");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "d2ebf9aefbaa416adcd0");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Add("Accept", "*/*");
            client.DefaultRequestHeaders.Add("Connection", "keep-alive");

            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                var content = new MultipartFormDataContent();
                content.Add(new StreamContent(fileStream), "file", filePath);
                content.Add(new StringContent("e8d002f9-f381-44c2-bce0-13416929f14d"), "Id");

                try
                {
                    var response = await client.PostAsync("<rest api end point>", content).ConfigureAwait(false);

                    if (response.IsSuccessStatusCode)
                    {
                        Debug.Write("Response received");
                    }
                }
                catch (Exception ex)
                {
                    Debug.Write("Exception occured");
                    Debug.Write(ex.Message);
                }
                finally
                {
                }
            }
        }
    }

It always goes to the exception block with exception as "The task was cancelled" 它总是转到带有异常的异常块,因为“任务已取消”

Not sure how can I debug it when it anyway works from the Postman. 不知道从邮递员那里无论如何都能调试它。

So the problem was related to ExpectContinue header which goes as true by default. 因此,问题与ExpectContinue标头有关,该标头默认情况下为true。 Somehow server was not handling it properly and client was waiting for continue (100) message for indefinite time. 服务器以某种方式无法正确处理它,并且客户端在不确定的时间内等待继续(100)消息。

For the time being manually setting this header to be false worked for us: 暂时手动将此标头设置为false可以为我们工作:

httpClient.DefaultRequestHeaders.ExpectContinue = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM