No connection could be made because the target machine actively refused it?
#1
I've encountered an issue when attempting to perform an HttpWebRequest to a local WebService. The specific error that I'm running into is a `System.Net.WebException` with an inner `System.Net.Sockets.SocketException` stating that the connection was refused on localhost port 80. The application is attempting to make a POST request using the `WebRequest` class. But it seems like the target machine is actively refusing the connection. I've ensured that the service is up and running, and that my firewall settings allow for local connections. I thought initially it could be a problem with the SSL certificate, so I added a `TrustAllCertificatePolicy`, but the issue persists. Below is the snippet of code that is throwing the exception. Any insights on why this could be happening would be greatly appreciated.

Code:
}
    }
} else {
    request.ContentLength = 0;
}
response = (HttpWebResponse) request.GetResponse();
Reply
#2
The error suggests that the service at localhost on port 80 is not accepting connections. This could be due to various reasons such as the server not running, the port being blocked, or the service not being bound to the correct IP address and port. Have you confirmed that your web service is definitely running and accessible via a browser or some other tool like curl or Postman? Moreover, the TrustAllCertificatePolicy is generally a bad practice and should be avoided due to potential security risks. Instead, you should ensure that your service has a valid SSL certificate if you're connecting over HTTPS.
Reply
#3
I second john_doe's comment on `TrustAllCertificatePolicy`, and I'd like to add that you should check if the web service is configured to accept HTTP POST requests. If the web service expects HTTPS and you're sending an HTTP request, the connection could be refused. Also, it's important to make sure that no other service is running on the same port and conflicting with your WebService. Lastly, check your application logs for more detailed error messages – they can provide additional context that we might be missing here. If possible, try to verify access to the web service using a minimal example to rule out issues with other parts of your code.
Reply
#4
Thanks for the suggestions. I checked the web service with Postman, and it seems accessible. I tried with a simple piece of code to perform a basic HTTP web request, and this time it worked perfectly. I am sharing the working code here. Perhaps we can compare it to see if there's something specific in my original code that's causing the issue.

Code:
using System;
using System.IO;
using System.Net;
class Program {
    static void Main() {
        string url = "http://localhost:80/service.asmx";
        string parameters = "param1=value1&param2=value2"; // Dummy parameters
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
        request.Method = WebRequestMethods.Http.Post;
        request.ContentType = "application/x-www-form-urlencoded";
        using(StreamWriter streamWriter = new StreamWriter(request.GetRequestStream())) {
            streamWriter.Write(parameters);
        }
        HttpWebResponse response = (HttpWebResponse) request.GetResponse();
        using(StreamReader streamReader = new StreamReader(response.GetResponseStream())) {
            string result = streamReader.ReadToEnd();
            Console.WriteLine(result);
        }
    }
}

Once I ensured that all services were correctly configured and running, the basic test succeeded, which makes me think there's something off with the logic I originally posted.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)