No connection could be made because the target machine actively refused it 127.0.0.1

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);
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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>

Method 2

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.

Method 3

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.

Method 4

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.

Method 5

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.

Method 6

I had the same issue with a site which previously was running fine. I resolved the issue by deleting the temporary files from C:WINDOWSMicrosoft.NETFrameworkv#.#.#####Temporary ASP.NET [email protected]###CC##C###C#CC

Method 7

Delete Temp files by run > %temp%

And Open VS2015 by run as admin,

it works for me.

Method 8

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

Method 9

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.

Method 10

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

Method 11

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x