19
votes

I try to connect to a web service hosted on a different server using the WebRequest Class. The web service returns a string as a Response. While doing so I get an Error:

"System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it"

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:14012 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at Limoallover.InsertSoapEnvelopeIntoWebRequestUS(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) at Limoallover.dawUpdateDriverStatus(String apiId, String apiKey, String idTrip, String tripCode, String statusCode) at Limoallover.UpdateJobStatus(String Lat, String Lng, Int32 DriverID, Int32 nJobStatus, Int32 nJobId, String CompanyID, String idTrip, String tripCode, String statusCode)

private  HttpWebRequest CreateWebRequestUS(string url, string action)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Headers.Add("SOAPAction", action);
    webRequest.ContentType = "text/xml;charset=\"utf-8\"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    return webRequest;
}

private  XmlDocument CreateSoapEnvelopeUS(string apiId, string apiKey, string idTrip, string tripCode, string statusCode)
{
    XmlDocument soapEnvelop = new XmlDocument();

    string xml = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";

    xml = xml + @"<soap:Body>";
    xml = xml + "<UpdateTripStatus xmlns=\"https://book.mylimobiz.com/api\">";
    xml = xml + @"<apiId>" + apiId + "</apiId>";
    xml = xml + @"<apiKey>" + apiKey + "</apiKey>";
    xml = xml + @"<idTrip>" + idTrip + "</idTrip>";
    xml = xml + @"<tripCode>" + tripCode + "</tripCode>";
    xml = xml + @"<statusCode>" + statusCode + "</statusCode>";
    xml = xml + @"</UpdateTripStatus>";
    xml = xml + @"</soap:Body>";
    xml = xml + @"</soap:Envelope>";



    soapEnvelop.LoadXml(xml);
    return soapEnvelop;
}

private static void InsertSoapEnvelopeIntoWebRequestUS(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
    using (Stream stream = webRequest.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }
}
11
127.0.0.1 does not seem different server to me?husnain_sys
127.0.0.1 redirects back to your own computer. firewall issue?Jeremy
127.0.0.1 is your local machine, aka "localhost".flayn
Make sure you are pointing to right web service addresshusnain_sys
Run netstat -ano from a command window to see if anything is listening on that address/port.500 - Internal Server Error

11 Answers

29
votes

After six days I find the answer which make me crazy! The answer is disable proxy at web.config file:

<system.net>
  <defaultProxy> 
    <proxy usesystemdefault="False"/> 
  </defaultProxy>
</system.net>
7
votes

The exception message says you're trying to connect to the same host (127.0.0.1), while you're stating that your server is running on a different host. Besides the obvious bugs like having "localhost" in the url, or maybe some you might want to check your DNS settings.

1
votes

There is a firewall blocking the connection or the process that is hosting the service is not listening on that port. Or it is listening on a different port.

1
votes

I had a similar issue, trying to run a WCF-based HttpSelfHostServer in Debug under my VisualStudio 2013. I tried every possible direction (turn off firewall, disabling IIS completely to eliminate the possibility localhost port is taken by some other service, etc.).

Eventually, what "did the trick" (solved the problem) was re-running VisualStudio 2013 as Administrator.

Amazing.

1
votes

The issue disappeared for me when I started Fiddler. Was using it on my machine before having the issue. Probably something with Fiddler being proxy.

1
votes

I had the same issue with a site which previously was running fine. I resolved the issue by deleting the temporary files from C:\WINDOWS\Microsoft.NET\Framework\v#.#.#####\Temporary ASP.NET Files\@projectName\###CC##C\###C#CC

0
votes

Delete Temp files by run > %temp%

And Open VS2015 by run as admin,

it works for me.

0
votes

If you have config file transforms then ensure you have the correct config selected within your publish profile. (Publish > Settings > Configuration)

0
votes

Had the same problem, it turned out it was the WindowsFirewall blocking connections. Try to disable WindowsFirewall for a while to see if helps and if it is the problem open ports as needed.

0
votes

If you have this while Fiddler is running -> in Fiddler, go to 'Rules' and disable 'Automatically Authenticate' and it should work again.

0
votes

Add new WebProxy() for the proxy setting , where you are creating a web request.

Example :-

  string url =  "Your URL";
  System.Net.WebRequest req = System.Net.WebRequest.Create(url);
  req.Proxy = new WebProxy();
  System.Net.WebResponse resp = req.GetResponse();

Where req.Proxy = new WebProxy() handle the proxy setting & helps the code to work fine.