none
Call functions with HttpClient RRS feed

  • Question

  • Hi all,

    I currently call my function with .Net HttpClient, but during the receive response step, a System.Net.Http.HttpRequestException occurred, content is SocketException: An existing connection was forcibly closed by the remote host, but this url works good when I open it in my browser, here are my code:

            public static string Authorize(string appName)
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add("Accept", "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8");
                    List<KeyValuePair<string, string>> enumerable = new List<KeyValuePair<string, string>>();
                    enumerable.Add(new KeyValuePair<string, string>("name", appName));
                    HttpContent cc = new FormUrlEncodedContent(enumerable.AsEnumerable());
                    HttpResponseMessage received = client.PostAsync("https://williamfunction.azurewebsites.net/api/xxx", cc).Result;
                    received.EnsureSuccessStatusCode();
                    string content = received.Content.ReadAsStringAsync().Result;
                    XmlDocument document = new XmlDocument();
                    document.LoadXml(content);
                    return document.DocumentElement.InnerText;
                }
            }

    please give me some advise.



    Friday, December 7, 2018 3:56 AM

Answers

All replies

  • I have added such headers but still got error:

                    client.DefaultRequestHeaders.Add("Accept", "text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8");
                    client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
                    client.DefaultRequestHeaders.Add("Accept-Language", "en-US; q=0.5, en; q=0.3");
                    client.DefaultRequestHeaders.Add("Cache-Control", "max-age=0");
                    client.DefaultRequestHeaders.Add("Connection", "keep-alive");
                    client.DefaultRequestHeaders.Add("Host", "williamfunction.azurewebsites.net");
                    client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

    Friday, December 7, 2018 5:32 AM
  • Content Removed
    Friday, December 7, 2018 5:50 AM
  • Hi Andrew,

    Finally I found the cause, if I changed the https to http in GetAsync function, everything works fine, I don't know why https works good in browser but doesn't work in HttpClient class.

    Friday, December 7, 2018 6:21 AM