httpClient.GetStringAsync(url).结果在Godaddy上部署时无法使用。

问题描述 投票:0回答:1

问题:httpClient.GetStringAsync(url).Result无法工作。该代码在本地主机上运行正常。但在GoDaddy上部署后就不行了。

下面是我的代码,在localhost上运行正常,但是部署到Godaddy上后,httpClient无法运行,即使我设置了SecurityProtocolType.Tls12。

public class MyWebClientServices
{
    private static HttpClient httpClient = new HttpClient();

    public T PrepareWebClient<T>(string preparedURL)
    {
        try
        {
          // As my server is using TLS1.2 I kept only that protocol
          ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

          httpClient.DefaultRequestHeaders.Accept.Clear();
          httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualtiyHeaderValue("application/json");
          var json = httpClient.GetStringAsync(preparedURL).Result;
          //here T is generic PCR class. I have created generic classes for which I need to invoke httpClient & their respective urls as I want to use this same code for all.

          T tJson = JsonConvert.DeserializeObject<T>(json);
          return (T)Convert.ChangeType(tJson, typeof(T));

         }
         catch(Exception e)
         {
            throw e;
         }
    }

}//end of MyWebClientServices class

在localhost中,我得到的数据很好.prepareURL被托管在一个服务器上,它有128AES TLS1.2 SSL.在Godaddy部署代码后,当我试图访问数据时,出现了以下错误

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


[SocketException (0x274c): 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]
   System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult) +6723762
   System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult) +57

[IOException: Unable to read data from the transport connection: 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.]
   System.Net.Security._SslStream.EndRead(IAsyncResult asyncResult) +6693442
   System.Net.TlsStream.EndRead(IAsyncResult asyncResult) +219
   System.Net.PooledStream.EndRead(IAsyncResult asyncResult) +15
   System.Net.Connection.ReadCallback(IAsyncResult asyncResult) +41

   [WebException: The underlying connection was closed: An unexpected error occurred on a receive.]
   System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) +638
   System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) +65

   [HttpRequestException: An error occurred while sending the request.]

我试着搜索了很多相关的问题,但得到的只是SMTP相关的问题& 答案,而我的问题是不同的,我无法找到任何类似的问题,因此在这里问它。

请帮助我如何在GoDaddy.我的主机是共享主机.SSL - Cloudflare SSLI检查与启用禁用SSL,但仍然问题相同。

c# model-view-controller httpclient tls1.2 godaddy-api
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.