vb.net - 在 GetReponse() 上获取 "The underlying connection was closed: An unexpected error occurred on a send"但不是通过浏览器获取

标签 vb.net

我正在尝试利用使用 GET 方法的 REST 服务。我正在使用.Net Framework 4.5.2。我下面写的只是一个测试,看看我是否真的可以提出请求。如果我将 URL 直接输入浏览器,我会得到良好的响应,其中包含 json 格式的字符串数据。但是当我尝试运行代码时,我最终收到以下错误:

“底层连接已关闭:发送时发生意外错误。” InnerException = {“无法从传输连接读取数据:现有连接被远程主机强制关闭。”}

我尝试将 keepalive 设置为 true 和 false,并且我也尝试使用 WebClient 和 DownloadString ....所有结果都会导致相同的错误。有人知道我缺少什么吗?或者这是服务器端的一个问题?

Dim script As String = "236"
Dim deploy As String = "1"
Dim compid As String = "915960_SB2"
Dim h As String = "value1"
Dim id As String = "1241389"
Dim status As String = "in freezer"
Dim request As HttpWebRequest
Dim response As HttpWebResponse

Try

'I have removed the real website name from this code.
            Dim theurl As String = "https://website/app/site/hosting/scriptlet.nl?script={0}&deploy={1}&compid={2}&h={3}&Id={4}&Status={5}"
            Dim url As String = String.Format(theurl, script, deploy, compid, h, id, status)

            request = HttpWebRequest.Create(url)
            request.Method = "GET"
            request.ContentType = "application/json"

'This is where the error occurs
            response = request.GetResponse()

Catch ex As Exception
            Dim test as string=""
Finally

End Try 

最佳答案

我解决了。我偶然发现以下网站,其他人也遇到了同样的问题。

http://www.avivroth.com/2013/05/02/rest-calls-in-net-c-over-ssl-https/

解决办法是设置安全协议(protocol)。我只是沿着列表从 ssl 到 tls11,然后到 tls12,发现最后一个有效。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

关于vb.net - 在 GetReponse() 上获取 "The underlying connection was closed: An unexpected error occurred on a send"但不是通过浏览器获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218430/

相关文章:

当表情符号在数据中时,JSON 字符串被截断

asp.net - .NET 接口(interface)签名

vb.net - 使用 TagLib 从 MP3 文件中提取专辑封面 - 是否有更好的方法编写此代码?

c# - 将 app.config 的 appsettings 部分分成两部分

asp.net - 将 PDF 保存到 DISC

c# - 仅使用新记录进行更新时,DataTable.Load 方法不起作用

c# - 使用 Silverlight/windows phone 样式控件设计我的 .Net 桌面应用程序 GUI

c# - 如何让我的控制台应用程序在不退出的情况下在另一个线程上运行后台进程?

vb.net - 显示两个 RichTextBox 控件之间的区别

sql - 从 sqlplus 调用存储过程