有时我得到以下错误,当我做HttpWebRequest到一个WebService。我也复制了下面的代码。


System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetRequestStream()

ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.PreAuthenticate = true;
request.Credentials = networkCredential(sla);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = v_Timeout * 1000;

if (url.IndexOf("asmx") > 0 && parStartIndex > 0)
{
    AppHelper.Logger.Append("#############" + sla.ServiceName);

    using (StreamWriter reqWriter = new StreamWriter(request.GetRequestStream()))
    {                        
        while (true)
        {
            int index01 = parList.Length;
            int index02 = parList.IndexOf("=");

            if (parList.IndexOf("&") > 0)
                index01 = parList.IndexOf("&");

            string parName = parList.Substring(0, index02);
            string parValue = parList.Substring(index02 + 1, index01 - index02 - 1);

            reqWriter.Write("{0}={1}", HttpUtility.UrlEncode(parName), HttpUtility.UrlEncode(parValue));

             if (index01 == parList.Length)
                 break;

             reqWriter.Write("&");
             parList = parList.Substring(index01 + 1);
         }
     }
 }
 else
 {
     request.ContentLength = 0;
 }

 response = (HttpWebResponse)request.GetResponse();

当前回答

我认为,你需要检查你的代理设置在“互联网选项”。如果您正在使用代理/'隐藏ip'应用程序,则可能会出现此问题。

其他回答

这可能是因为授权问题;我就是这样。 例如,如果在控制器函数的顶部有[Authorize("WriteAccess")]或[Authorize("ReadAccess")],请尝试将它们注释掉。

用于解决方案中的服务引用。

重新启动工作站 重新构建解决方案 更新WCFclient项目中的服务引用

此时,我收到消息(Windows 7)允许系统访问。 然后正确地更新了服务引用,没有出现错误。

最可能的原因是防火墙。

这篇文章包含了一组可能对您有用的原因。

从文章来看,可能的原因有:

FTP服务器设置 软件/个人防火墙设置 多种软件/个人防火墙 杀毒软件 LSP层 路由器固件 关闭电脑 电脑未插电 提琴手

在我的情况下,这是由一个错误的部署,在我的网络设置。没有配置。

一位同事解释说,错误消息中的IP地址代表本地主机。

当我修正网页的时候。我当时使用正确的url进行服务器调用,它工作。

我想我应该把这个贴出来,也许能帮助到一些人。

有一种服务叫做“SQL Server浏览器”,它向客户端提供SQL Server连接信息。

在我的例子中,现有的解决方案都不起作用,因为这个服务没有运行。我恢复了它,一切都恢复得很完美。