Error in project: Exception thrown: 'System.Net.WebException' in System.dll

33 Views Asked by At

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?

0

There are 0 best solutions below