0

There is a POST rest api which used to work from code before. 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.

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

 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.

Randeep Singh
  • 1,275
  • 14
  • 29
  • 1
    I'm not sure if this will help you, but I had a similar experience the response I was receiving was "The task was cancelled" as part of an AggregateException. The issue for me was a timeout issue. If it helps, take a look at my [answer to a similar question](https://stackoverflow.com/a/33096894/685760) or at least the other answers to that similar question. It may provide you other things to try. – Mr Moose Jun 29 '17 at 09:08
  • 1
    there's probably an innerexception - have you looked for that? Set a breakpoint and examine the exception object – ADyson Jun 29 '17 at 09:09
  • Ohk , so I enabled the exception and can see that there is an object disposed exception, it says that can't access the object which is disposed. Probably the link shared might help me. I will just check it. – Randeep Singh Jun 29 '17 at 09:36
  • I removed the using statement for httpClient but still see the same exception of disposed Object can't be accessed. – Randeep Singh Jun 29 '17 at 09:43
  • if you temporarily remove your try/catch it'll break on the actual line where this is happening. Then you can hopefully narrow it down to the right object. – ADyson Jun 29 '17 at 09:56
  • @ADyson I have tried that too, but no luck, so basically I can see the execution going till await call, but after that there is no response coming back. – Randeep Singh Jun 29 '17 at 10:19
  • Do any calls in the stack block on async? – Crowcoder Jun 29 '17 at 12:21
  • @Randeep - Did you `client.Timeout` to some larger value to allow time for the file to upload. Look at [Remarks section](https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.timeout(v=vs.110).aspx#Remarks) of HttpClient.Timeout as it has some interesting information regarding DNS resolution taking up to 15 seconds of the default value for Timeout of 100 seconds. – Mr Moose Jun 30 '17 at 02:21
  • @Crowcoder Yes, I am calling this method from Main and there there I am blocking on this method. MrMoose I have tried with 10 min timeout but still the same outcome. – Randeep Singh Jul 12 '17 at 00:51
  • Ok, don't do that. If you can't use async all the way down then use non-async HttpWebRequest instead. – Crowcoder Jul 12 '17 at 01:09

1 Answers1

2

So the problem was related to ExpectContinue header which goes as true by default. Somehow server was not handling it properly and client was waiting for continue (100) message for indefinite time.

For the time being manually setting this header to be false worked for us:

httpClient.DefaultRequestHeaders.ExpectContinue = false;
Rob
  • 26,989
  • 16
  • 82
  • 98
Randeep Singh
  • 1,275
  • 14
  • 29