HttpWebRequest Failing (Unable to connect to the remote server) WinForm V WebForms

0

I have a strange problem. I have WinForms app that executes a webRequest

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://*********");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = WebRequestMethods.Http.Post;

This works perfectly. I now have a WebForms application that uses exactly the same code. It compiles, but running it results in the following exception:

System.Net.WebException
HResult=0x80131509
Message=Unable to connect to the remote server

Any clues why it would in one and not the other. Its running in a Visual Studio IIS environment (makes no difference if I use IE or Chrome.

Here is the code:

private void GetSession()
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://************");

        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = WebRequestMethods.Http.Post;

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = new JavaScriptSerializer().Serialize(new
            {
                operation = "****",
                username = "****",
                password = "****"
            });

            streamWriter.Write(json);
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        JavaScriptSerializer ser = new JavaScriptSerializer();

        UserSession =
            ser.Deserialize<JsonSession>(new StreamReader(httpResponse.GetResponseStream(), Encoding.Default).ReadToEnd());
    }
c#
iis
httpwebrequest
asked on Stack Overflow Apr 11, 2018 by user9447717 • edited Apr 11, 2018 by user9447717

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0