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

当前回答

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

其他回答

我经常遇到这种问题。我发现SQL Server代理服务没有运行。当我手动启动这项服务时,问题就解决了。再次检查服务是否在运行:

运行提示符,输入services。MSC,然后回车 查找服务名称- SQL Server代理(实例名)

如果SQL Server Agent未运行,双击该服务打开属性窗口。然后点击开始按钮。希望它能帮助到一些人。

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

这是非常具体的,但是如果你在尝试使用mongo连接到数据库后收到这个错误,对我来说,在运行mongo.exe之前运行mongo.exe,然后连接工作正常。希望这能帮助到一些人。

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

I was facing this issue today. Mine was Asp.Net Core API and it uses Postgresql as the database. We have configured this database as a Docker container. So the first step I did was to check whether I am able to access the database or not. To do that I searched for PgAdmin in the start as I have configured the same. Clicking on the resulted application will redirect you to the http://127.0.0.1:23722/browser/. There you can try access your database on the left menu. For me I was getting an error as in the below image.

输入密码,并尝试是否能够访问它。对我来说,这行不通。因为它是一个Docker容器,我决定重新启动我的Docker桌面,右键单击任务栏中的Docker图标,然后单击重新启动。

在重新启动Docker后,我能够登录并看到数据库,并且当我在Visual Studio中重新启动应用程序时,错误也消失了。

希望能有所帮助。