HttpWebRequest失败(无法连接到远程服务器)WinForm V WebForms

时间:2018-04-11 10:43:27

标签: c# iis httpwebrequest

我有一个奇怪的问题。我有运行webRequest的WinForms应用程序

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

这完美无缺。我现在有一个使用完全相同代码的WebForms应用程序。它编译,但运行它会导致以下异常:

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

任何线索,为什么它会在一个而不是另一个。它在Visual Studio IIS环境中运行(如果我使用IE或Chrome,则没有任何区别。

以下是代码:

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());
    }

0 个答案:

没有答案