19

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);
    }
}
Gareth
  • 5,140
  • 5
  • 42
  • 73
Ahmed Said
  • 497
  • 1
  • 4
  • 11
  • shutdown applications like TeamViewer, BitTorrent – TIKSN Feb 05 '15 at 14:13
  • 3
    127.0.0.1 does not seem different server to me? – husnain_sys Feb 05 '15 at 14:14
  • 1
    127.0.0.1 redirects back to your own computer. firewall issue? – Jeremy Feb 05 '15 at 14:15
  • 1
    127.0.0.1 is your local machine, aka "localhost". – flayn Feb 05 '15 at 14:16
  • 2
    Make sure you are pointing to right web service address – husnain_sys Feb 05 '15 at 14:17
  • 2
    Run `netstat -ano` from a command window to see if anything is listening on that address/port. – 500 - Internal Server Error Feb 05 '15 at 14:50
  • @FlorianGerhardt i did – Ahmed Said Feb 05 '15 at 14:57
  • As you point out here: https://stackoverflow.com/a/28346329/173711 "thanks for reply but the same error appears also i wanna tell you it working fine on my machine once publishing to server this problem appears " the problem appears after you pushed it to the server. You need to enter the address of the server, instead of 127.0.0.1. – flayn Feb 05 '15 at 15:01
  • @FlorianGerhardt i do not know where is 127.0.0.1 located – Ahmed Said Feb 05 '15 at 15:03
  • @AhmedSaid: When you create your webrequest, YOU specifiy where to connect to. Currently is seems to say: 127.0.0.1. – flayn Feb 05 '15 at 15:52
  • @FlorianGerhardt check below please – Ahmed Said Feb 05 '15 at 15:59
  • var _url = "https://book.mylimobiz.com/api/apiservice.asmx"; var _action = "https://book.mylimobiz.com/api/UpdateTripStatus"; XmlDocument soapEnvelopeXml = CreateSoapEnvelopeUS(apiId, apiKey, idTrip, tripCode, statusCode); HttpWebRequest webRequest = CreateWebRequestUS(_url, _action); InsertSoapEnvelopeIntoWebRequestUS(soapEnvelopeXml, webRequest); IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null); – Ahmed Said Feb 05 '15 at 16:00
  • @AhmedSaid: Very strange, please add the code of 'CreateWebRequestUS' and 'InsertSoapEnvelopeIntoWebRequestUS' to your question. – flayn Feb 05 '15 at 21:08
  • @FlorianGerhardt HYR and really thanks too much if you have facebook account i can add you i updated the code – Ahmed Said Feb 06 '15 at 09:46

11 Answers11

29

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>
Click Ok
  • 8,700
  • 18
  • 70
  • 106
Ahmed Said
  • 497
  • 1
  • 4
  • 11
7

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.

michaelb
  • 451
  • 2
  • 11
1

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.

husnain_sys
  • 571
  • 4
  • 14
1

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

DeveloperDan
  • 4,626
  • 9
  • 40
  • 65
1

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.

datps
  • 768
  • 1
  • 6
  • 16
1

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.

0

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.

Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50
  • thanks for reply but the same error appears also i wanna tell you it working fine on my machine once publishing to server this problem appears – Ahmed Said Feb 05 '15 at 14:54
  • ok, just remember that actively refused means the client connected and was shut down for some reason, so the issue has to be some policy on the server or something like that; don't get mad with client configuration it is probably ok – Mauro Sampietro Feb 05 '15 at 14:59
  • it made me crazy can you check it to me you can connect to my mac via teamviewer – Ahmed Said Feb 05 '15 at 15:00
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – David says Reinstate Monica Feb 05 '15 at 17:07
  • @DavidGrinberg that was the answer in my case and i shared it. – Mauro Sampietro Feb 06 '15 at 09:42
0

Delete Temp files by run > %temp%

And Open VS2015 by run as admin,

it works for me.

Saurin Vala
  • 1,898
  • 1
  • 17
  • 23
0

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

Ian
  • 2,898
  • 1
  • 27
  • 29
0

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

Immi
  • 127
  • 7
0

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.

Aakash Singh
  • 1,032
  • 8
  • 27