gpt4 book ai didi

c# - 使用 Webclient 获取对 url 的请求会抛出异常,但如果在浏览器中打开它则工作正常

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:33 25 4
gpt4 key购买 nike

我有一个很奇怪的问题。我正在尝试使用 WebClient 向 url 发出获取请求

WebClient client = new WebClient(); 
client.DownloadString("https://api.test.kount.net/rpc/v1/orders/detail.xml");

它抛出以下异常。

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: The decryption operation failed, see inner exception. ---> 

System.ComponentModel.Win32Exception: The message received was unexpected or badly formatted --- End of inner exception stack trace --- at System.Net.Security._SslStream.ProcessReadErrorCode(SecurityStatus errorCode, Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest,

Byte[] extraBuffer) at System.Net.Security._SslStream.ProcessFrameBody(Int32 readBytes, Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)

但是如果我在浏览器中打开这个 url,它会打开并返回一个 xml 响应。我错过了什么?有什么想法吗?

最佳答案

Right. I want to get this error message.

捕获异常并读取内容。

var responseStr = "";
try
{
WebClient client = new WebClient();
responseStr = client.DownloadString("https://api.test.kount.net/rpc/v1/orders/detail.xml");
}catch(WebException wex)
{
responseStr = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
}

另一种选择

HttpClient httpClient = new HttpClient();
var resp = await httpClient.GetAsync("https://api.test.kount.net/rpc/v1/orders/detail.xml");
var status = resp.StatusCode;
responseStr = await resp.Content.ReadAsStringAsync();

所有这些都有效...

关于c# - 使用 Webclient 获取对 url 的请求会抛出异常,但如果在浏览器中打开它则工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31629206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com