使用 VB.Net 发送 Post 请求
Sending a Post request using VB.Net
我想从服务器端向另一台服务器发送一个Post请求。我想在代码中创建一些表单数据(不使用网页)并将其发送过来。
根据我在网上阅读的内容,我得到了以下代码。但是,我只是在猜测,不确定它是否正确,尤其是因为我无法让它工作(我得到的异常已作为注释包含在代码中)。这是我的错误还是与我发送请求的位置有关的外部问题?
Dim client = New HttpClient
Dim request = WebRequest.CreateHttp("https://something.com/test")
request.Credentials = CredentialCache.DefaultCredentials
request.UserAgent = "value"
request.Method = HttpMethod.Post.Method
request.ContentType = "application/x-www-form-urlencoded"
Dim params = New Dictionary(Of String, String)
params.Add("key1", "value1")
params.Add("key2", "value2")
params.Add("key3", "value3")
params.Add("key4", "value4")
Dim stream = request.GetRequestStream()
Dim content = New FormUrlEncodedContent(params)
content.CopyToAsync(stream)
' Exception occurs when executing the line below:
' The underlying connection was closed: An unexpected error occurred on a send.
' InnerException = {"Unable to read data from the transport connection:
' An existing connection was forcibly closed by the remote host."}
Dim result = request.GetResponseAsync().Result
Console.WriteLine(result.ToString)
您确定服务器具有有效的 https 证书吗?
- 证书已颁发给您正在访问的 URI
- 证书未过期
- 证书是由受信任的机构颁发的(例如:Verisign)
在这些条件中,#3 是最常失败的检查。您可以通过编程忽略任何或所有这些错误(风险自负)。这是一个关于如何做到这一点的例子。 (参考:)
此外,提供完整的(内部)异常也会很有帮助
我想从服务器端向另一台服务器发送一个Post请求。我想在代码中创建一些表单数据(不使用网页)并将其发送过来。
根据我在网上阅读的内容,我得到了以下代码。但是,我只是在猜测,不确定它是否正确,尤其是因为我无法让它工作(我得到的异常已作为注释包含在代码中)。这是我的错误还是与我发送请求的位置有关的外部问题?
Dim client = New HttpClient
Dim request = WebRequest.CreateHttp("https://something.com/test")
request.Credentials = CredentialCache.DefaultCredentials
request.UserAgent = "value"
request.Method = HttpMethod.Post.Method
request.ContentType = "application/x-www-form-urlencoded"
Dim params = New Dictionary(Of String, String)
params.Add("key1", "value1")
params.Add("key2", "value2")
params.Add("key3", "value3")
params.Add("key4", "value4")
Dim stream = request.GetRequestStream()
Dim content = New FormUrlEncodedContent(params)
content.CopyToAsync(stream)
' Exception occurs when executing the line below:
' The underlying connection was closed: An unexpected error occurred on a send.
' InnerException = {"Unable to read data from the transport connection:
' An existing connection was forcibly closed by the remote host."}
Dim result = request.GetResponseAsync().Result
Console.WriteLine(result.ToString)
您确定服务器具有有效的 https 证书吗?
- 证书已颁发给您正在访问的 URI
- 证书未过期
- 证书是由受信任的机构颁发的(例如:Verisign)
在这些条件中,#3 是最常失败的检查。您可以通过编程忽略任何或所有这些错误(风险自负)。这是一个关于如何做到这一点的例子。 (参考:)
此外,提供完整的(内部)异常也会很有帮助