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

当前回答

这对我来说是一个愚蠢的问题,我已经添加了一个defaultproxy到我的网络。配置,以拦截流量在提琴手,然后忘记删除它!

其他回答

我现在就面对这个问题……

在我这边,我有2个独立的Visual Studio解决方案(.sln)…在各自的Visual Studio实例中打开每个实例。

解决方案2调用解决方案1代码。问题与分配给解决方案1的端口有关。我不得不将解决方案1上的端口更改为另一个端口,然后解决方案2又开始工作了。因此,请确保检查分配给项目的端口。

我有同样的错误与我的WCF服务使用Net TCP绑定,但解决后启动以下服务在我的情况。

Net.Pipe.Listener.Adapter

Net.TCP.Listener.Adapter

网Tcp端口共享服务

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

通常,连接脚本不会提到要使用的端口。例如:

$mysqli = mysqli_connect('127.0.0.0.1', 'user', 'password', 'database');

所以,要连接到一个不使用端口3306的管理器,你必须在连接请求上指定端口号:

$mysqli = mysqli_connect('127.0.0.0.1', 'user', 'password', 'database', '3307');

要检查MySQL或MariaDB数据库管理器上的连接,使用脚本: 里面(64)\ www \ testmysql.php 在浏览器地址栏中输入“http://localhost/testmysql.php”,然后根据参数修改脚本。

我遇到了这个错误,花了一些时间来解决它。在我的情况下,我有https和net。tcp配置为相同端口上的IIS绑定。显然,你不能在同一个端口上有两个东西。我使用netstat -ap tcp命令检查该端口上是否有侦听。没有人在听。删除不必要的绑定(在我的情况下是https)解决了我的问题。