有时我得到以下错误,当我做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();

当前回答

在我的例子中,有些域名可以使用,而有些则不行。在我的网页中添加对我的组织的代理Url的引用。配置修复了这个问题。

<system.net>
    <defaultProxy useDefaultCredentials="true">
      <proxy proxyaddress="http://proxy.my-org.com/" usesystemdefault="True"/>
    </defaultProxy>
</system.net>

其他回答

对我来说,我想在shell中启动mongo(与问题的确切上下文无关,但在shell中启动mongo之前有相同的错误消息)

进程“MongoDB Service”没有在服务中运行

以管理员身份启动cmd并输入,

net start MongoDB

为了查看MongoDB是否启动并运行,只需输入mongo,在cmd中,它会给出mongo版本详细信息和mongo连接URL

我在一个使用AppFabric的应用程序中得到了这个错误。线索是在堆栈跟踪中获得一个DataCacheException。要查看这是否是您的问题,请运行以下PowerShell命令:

@("AppFabricCachingService","RemoteRegistry") | % { get-service $_ }

如果这两个服务中的任何一个停止了,那么您将得到这个错误。

在我的场景中,我有两个应用程序:

App1 App2

假设:App1应该监听App2在端口5000上的活动 错误:启动App1并试图监听一个不存在的鬼城,会产生错误 解决方案:首先启动App2,然后尝试使用App1监听

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

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

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

我从我的web层引用位于WCFHost上的服务时收到了这个错误。对我有效的方法可能并不适用于所有人,但我把这个答案留给那些可能适用的人。我的WCFHost的端口号是由IIS随机更新的,我只需要在我的web配置中更新到svc引用的结束路由。问题解决了。