asp.net C#无法连接到远程服务器(发生异常)

vc6uscn9  于 2023-03-13  发布在  .NET
关注(0)|答案(1)|浏览(566)

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();
uttx8gqw

uttx8gqw1#

我遇到了同样的问题,终于找到了障碍。很明显,HttpClient的默认端口是443,但它在Web服务器上是关闭的。尝试通过Windows上的CURL命令调用您的API Uri地址,以检查此端口是否打开,如下所示:
curl https://api.xxx.com/api/...
如果你通过在浏览器中调用你的地址得到响应,请确保在你的浏览器上设置了代理, Postman 也以同样的方式调用地址。

相关问题