Ahmed Said Ahmed Said - 3 years ago 229
ASP.NET (C#) Question

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

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 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
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>";

return soapEnvelop;

private static void InsertSoapEnvelopeIntoWebRequestUS(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
using (Stream stream = webRequest.GetRequestStream())

Answer Source

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

    <proxy usesystemdefault="False"/> 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download