none
HttpWebRequest.GetResponse() method works when run in Azure CLI but not when published to Azure portal RRS feed

  • Question

  • I have the following C# code developed in VS2017 (15.3.0 preview 2.1).  When I run/debug locally executing from the Azure CLI it works properly.  But when I publish to the Azure Functions portal it generates this error: "The underlying connection was closed: An unexpected error occurred on a send." on this line of code.

    response = (HttpWebResponse)request.GetResponse();

    I'm using Postman to test with both Azure CLI and Azure Functions portal using identical inputs.

    Here is the full method for context:

    private static string GetPerDiemJson(string zip, string fiscalYear, TraceWriter log)
            {
                HttpWebResponse response = null;
                Stream receiveStream = null;
                StreamReader streamReader = null;
                try
                {
                    string filters = string.Format("&filters={{\"FiscalYear\":\"{0}\",\"Zip\":\"{1}\"}}", fiscalYear, zip);
                    string resourceUrl = string.Format("{0}{1}{2}", GSA_REST_URL, RESOURCEID, filters);
    
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resourceUrl);
    
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
    
                    //This is the line that errors...
                    response = (HttpWebResponse)request.GetResponse();  
    
                    receiveStream = response.GetResponseStream();
                    streamReader = new StreamReader(receiveStream, Encoding.UTF8);
                    string jsonResponse = streamReader.ReadToEnd();
    
                    response.Close();
                    streamReader.Close();
                    return jsonResponse;
                }
                catch (Exception e)
                {
                    throw;
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }
                    if (streamReader != null)
                    {
                        streamReader.Close();
                    }
                }
            }



    Tom Castiglia Hershey Technologies


    Sunday, June 25, 2017 5:24 AM

Answers

  • Well, nevermind...  I figured this one out.  I just needed to add the following line of statement after WebRequest.Create():

                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                                            | SecurityProtocolType.Tls11
                                                            | SecurityProtocolType.Tls12
                                                            | SecurityProtocolType.Ssl3;



    Tom Castiglia Hershey Technologies

    • Marked as answer by TomCastiglia Monday, June 26, 2017 5:48 AM
    Monday, June 26, 2017 5:48 AM

All replies

  • Well, nevermind...  I figured this one out.  I just needed to add the following line of statement after WebRequest.Create():

                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                                            | SecurityProtocolType.Tls11
                                                            | SecurityProtocolType.Tls12
                                                            | SecurityProtocolType.Ssl3;



    Tom Castiglia Hershey Technologies

    • Marked as answer by TomCastiglia Monday, June 26, 2017 5:48 AM
    Monday, June 26, 2017 5:48 AM
  • Glad you got it sorted out. In general we recommend using HttpClient rather than HttpWebRequest but I am not sure if that would have made a difference in this particular case.
    Monday, June 26, 2017 8:18 PM
  • Paul - Thanks for the insight.  I'll plan to update my code to use HttpClient instead.

    Tom Castiglia Hershey Technologies

    Monday, June 26, 2017 8:30 PM