0

I have project written by C#, using .NET framework 2.0. I have function for sending request to API url This is my code:

 static string SendRequest4(string url, string dataCont)
 {
     try
     {
         // Tạo URL mới bằng cách thêm tham số "data" vào URL
         string requestUrl = url + "?data=" + dataCont;
         Console.WriteLine("url: " + requestUrl);
         // Tạo một đối tượng WebClient
         WebClient client = new WebClient();

         // Gửi yêu cầu và nhận phản hồi từ máy chủ
         byte[] responseData = client.DownloadData(requestUrl);

         // Chuyển đổi dữ liệu nhận được sang chuỗi và trả về
         return System.Text.Encoding.UTF8.GetString(responseData);
     }
     catch (WebException ex)
     {
         // Xử lý ngoại lệ nếu có
         Console.WriteLine("WebException: " + ex.Message);
         return null; // Trả về null nếu có lỗi xảy ra
     }
     catch (Exception ex)
     {
         // Xử lý ngoại lệ chung nếu có
         Console.WriteLine("Exception: " + ex.Message);
         return null; // Trả về null nếu có lỗi xảy ra
     }
 }

But it error when sending request. This is error:

Exception thrown: 'System.Net.WebException' in System.dll
WebException: The underlying connection was closed: An unexpected error occurred on a send.

I have searched for many solutions but none of them are effective other than upgrading .NET Framework to a higher version, but I want to use version 2.0. Is there any alternative without having to upgrade?

7
  • Have you checked what the cause (in your specific case) is? Is there a problem with the request? You can get the response details using this code. Commented Mar 22, 2024 at 1:30
  • 1
    The .NET Framework version 2.0 has been out of support for probably more than a decade. But, if you upgrade to the Framework v3.51, you'll get the core of .NET 2.0 included (3.51 is a superset of 2.0 with bug fixes). Its support depends on the OS you run ot on (I believe it's supported on Win 10, but not Win 11). All .NET 2.0 apps should just work the same way on 3.51 (give or take a bug fix or two) Commented Mar 22, 2024 at 1:37
  • I forgot to ask... Why are you so attached to a software Framework that shipped in 2005? Commented Mar 22, 2024 at 1:38
  • in my specific case is "Exception thrown: 'System.Net.WebException' in System.dll WebException: The underlying connection was closed: An unexpected error occurred on a send. WebExceptionStatus: SendFailure" @ProgrammingLlama Commented Mar 22, 2024 at 1:38
  • @Flydog57 thanks, i tried to upgrade to .net framework 4.0 but still got error. "Exception thrown: 'System.Net.WebException' in System.dll Error: The request was aborted: Could not create SSL/TLS secure channel." Commented Mar 22, 2024 at 1:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.