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

其他回答

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

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

我遇到了同样的错误,因为当你的服务器和客户端运行在同一台机器上,客户端需要服务器本地ip地址而不是公共ip地址来与服务器通信,你只需要公共ip地址,当服务器和客户端运行在不同的机器上,所以在客户端程序中使用本地ip地址来连接服务器,使用这种方法可以找到本地ip地址。

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

            return "null";
        }

    }

在Windows 7 Home Premium 64bit上使用WampServer 64bit时,我遇到了这个确切的问题。经过一小时又一小时的试验,很明显,我们所需要的只是在my.ini中注释掉一行。然后它工作得很好。

注释掉1行 套接字= mysql

如果你把旧的/data/文件放在合适的位置,WampServer将接受所有这些文件,除了它要重写的/mysql/文件夹。因此,然后我简单地从我以前的开发环境中导入/mysql/ user数据的备份,并在phpMyAdmin SQL窗口中运行FLUSH PRIVILEGES。伟大的工作。一定是出了什么问题,因为事情不应该这么容易。

转到你的WCF项目- 属性- > -> 调试器 ->取消复选框的标记

启用“编辑并继续”

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

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