0

I am trying to get data from Azure function APP method (this Get API URL returns the data when directly entered in browser), but failing when I am trying to call this URL in my C# code with following inner exception:

InnerException {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."} System.Exception {System.IO.IOException}

Following is the code:

string html = string.Empty;
        string url = @"https://mgyapi.azurewebsites.net/api/my_get/";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url+ "421?code=xxxxxxxxxxwBuTCJpOIVmqBS8DIgE4MhTA==");
        request.AutomaticDecompression = DecompressionMethods.GZip;
        request.Method = "GET";
        //request.ContentType = contentType;
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36";
        request.CookieContainer = new CookieContainer();
        //request.ContentLength = formData.Length;
        request.Accept = "*/*";
        request.Host = "mgyapi.azurewebsites.net";
        request.KeepAlive = true;
        request.Timeout = 999999999;
        // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) //<----Here exception raises
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            html = reader.ReadToEnd();
        }

Following is the Request Header I see in the browser:

Accept: text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,image / apng,*/*;q=0.8,application/signed-exchange;v=b3
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
    Cache-Control: max-age=0
    Connection: keep-alive
    Host: mgyapi-v1.azurewebsites.net
    Sec-Fetch-Mode: navigate
    Sec-Fetch-Site: none
    Sec-Fetch-User: ?1
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
user1400290
  • 1,682
  • 5
  • 23
  • 43
  • Did you check whether the URL is working properly or not? – Rohan Rao Oct 18 '19 at 09:13
  • Can you compare the `Header` sent by browser when invoked directly vs code? – user1672994 Oct 18 '19 at 09:19
  • @Rohan Rao - I have checked that the URL is working directly in browser. – user1400290 Oct 18 '19 at 09:43
  • @user1672994 - I have added the Request headers seen in the browser. – user1400290 Oct 18 '19 at 09:49
  • @user1400290 - Is Azure function app require minimum version of TLS as 1.2. If no, then change the code as `System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12'` – user1672994 Oct 18 '19 at 11:19
  • Also, you don't need to specify `UserAgent `, `CookieContainer `, `Host ` and `Timeout `.If your API returns Json, then define the Contect-Type as application/json. – user1672994 Oct 18 '19 at 11:20
  • There's a known [issue](https://github.com/Azure/azure-functions-host/issues/1639#issuecomment-320125484) about this, add `ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;` or change Tls to 1.2 in the Azure Function Network SSL. – George Chen Oct 21 '19 at 02:45

1 Answers1

0

There are three such methods in ASP.Net for webrequest.

  1. HttpWebRequest
  2. WebRequest
  3. HttpRequest

I am not pointing the difference for the above items. You can find it from here: Difference between HttpWebRequest and WebRequest in ASP.Net

Replace the HttpWebRequest with just WebRequest and replace all the HttpWebResponse with WebResponse and see what output you are getting. I think this should solve your problem.

Rohan Rao
  • 2,505
  • 3
  • 19
  • 39