有时我得到以下错误,当我做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地址而不是公共ip地址来与服务器通信,你只需要公共ip地址,当服务器和客户端运行在不同的机器上,所以在客户端程序中使用本地ip地址来连接服务器,使用这种方法可以找到本地ip地址。

 public static string Getlocalip()
    {
        try
        {
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            return localIPs[7].ToString();
        }
        catch (Exception)
        {

            return "null";
        }

    }

其他回答

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

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

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

net start MongoDB

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

最可能的原因是防火墙。

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

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

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

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

App1 App2

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

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

$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”,然后根据参数修改脚本。

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

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