Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Unable to connect to remote server (WebException, SocketException)

I try to POST some data from a website to a webservice, but i got those exceptions :

System.AggregateException: Une ou plusieurs erreurs se sont produites. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond 52.211.194.210:443 à System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) à System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- Fin de la trace de la pile d'exception interne --- à System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) à System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) --- Fin de la trace de la pile d'exception interne --- --- Fin de la trace de la pile d'exception interne --- à System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) à ldspoPunchout.ldspoPunchout.PostXMLData(String destinationUrl, String requestXml) ---> (Exception interne #0) System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Impossible de se connecter au serveur distant ---> System.Net.Sockets.SocketException: Une tentative de connexion a échoué car le parti connecté n’a pas répondu convenablement au-delà d’une certaine durée ou une connexion établie a échoué car l’hôte de connexion n’a pas répondu 52.211.194.210:443 à System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) à System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- Fin de la trace de la pile d'exception interne --- à System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) à System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) --- Fin de la trace de la pile d'exception interne ---<---

my code is simple and worked out in multiple sites :

            byte[] data = Encoding.ASCII.GetBytes(requestXml);
            ByteArrayContent byteArray = new ByteArrayContent(data);
            HttpResponseMessage reponse = client.PostAsync(destinationUrl, byteArray).Result;
            Stream stream = reponse.Content.ReadAsStreamAsync().Result;
            StreamReader reader = new StreamReader(stream);
            Test = reader.ReadToEnd();

this code use httpclient but i tried webrequest too, same issue

environment : windows server 2012 R2 standard

i don't have access to the code of the webservice but it is a HTTPS

i'm confuse because on my local machine (windows 10) i can browse,post,get the url, even on the server i can browse the url with IE and it has no proxy setting

EDIT : forgot to mention that i can post on other website from the server !

i thought it was a time out but when adding those lines :

            client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);

or

      client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
            client.DefaultRequestHeaders.Add("Keep-Alive", "3600");

nothing change and it is rather quick(like 15sec) no matter how much time i set and tried various other things i read :

              ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 
           | SecurityProtocolType.Tls11;
            client.DefaultRequestHeaders.ExpectContinue = false;

if someone knows where could this come from that'd be great

Thanks in advance

EDIT : i create the webrequest like this

                Logger.Log(xml + "                 " + url);
                var webRequest = WebRequest.Create(url);
                PostXMLData(url, xml); 
                return url + "  :  " + Test;

http client is set at higher level :

       public static readonly HttpClient client = new HttpClient();
like image 526
loxod Avatar asked Feb 05 '26 17:02

loxod


1 Answers

I had the same problem, finally figured out the obstacle. It is clear that HttpClient default Port is 443 but it is Closed on Web Server. Try to call your api Uri Address via CURL command on windows to check if this port is open or not like this :

Curl https://api.xxx.com/api/....

if you get response by calling your address in browser be sure a proxy is set on your browser also PostMan calls the address the same way.

like image 122
Farshad Nasehi Avatar answered Feb 07 '26 07:02

Farshad Nasehi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!