我想从服务器端向另一台服务器发送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)
答案 0 :(得分:0)
您确定服务器具有有效的https证书吗?
在这些标准中,#3是最常见的失败检查。您可以以编程方式忽略任何或所有这些错误(风险自负)。这是一个如何做到这一点的例子。 (参考:https://stackoverflow.com/a/10390388/8081260)
提供完整(内部)异常
也会有所帮助