0
我想从服务器端发送Post请求到另一台服务器。我想在代码中创建一些表单数据(不使用网页)并发送。使用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)
好的谢谢。这是内部的例外(在代码注释中)。这是非常基本的。堆栈跟踪显示更多,但我认为你是正确的URL导致问题,也许在另一台服务器端(如防火墙问题)。我主要担心这可能是我的问题,因为我从来没有使用过HttpClient/WebRequest。 – Mayron