HttpWebRequest 无法连接到远程服务器

先写个例子:

// string url = "http://127.0.0.1"

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
request.Timeout = 6000;


HttpWebResponse response = null;
try
{
      StreamWriter sw = newStreamWriter(request.GetRequestStream());

  ...

}

catch(Exception ex)

{

  return ex.Message;    // 无法连接到远程服务器

}

然后尝试在网页中输入“http://127.0.0.1”或者"http://localhost/"均被告知无法连接。解决方法是:

本人使用的是win7。打开控制面板 -> 程序 -> 打开或关闭windows功能,如果所示:

稍等片刻,勾选“Internet信息服务”系统会默认勾选上“Web管理工具”和“万维网服务”后,系统会进行自动更新安装。成功后,再次输入“http://127.0.0.1”或“http://localhost/”出现如下效果:

表示OK!

原文地址:https://www.cnblogs.com/SkyflyBird/p/4813440.html