我需要使用一个需要 JSON 字符串的 Java 应用程序(使用 spring 和 hibernate 制作)在 VB.NET 中向 Tomcat 服务器发出 POST 请求。我是 VB.NET 的新手,我不知道如何做到这一点。
这是我在 VB.NET 中的代码(是一个用于智能设备的应用程序,因此我使用 Compact Framework 3.5)
Private Sub BtnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLogin.Click
Dim usuario = TextUsuario.Text
Dim contraseña = TextContraseña.Text
Dim userCredentials = New Linq.JObject
If usuario IsNot "" And contraseña IsNot "" Then
userCredentials.Add("login", usuario)
userCredentials.Add("password", contraseña)
Dim userCredentialsJSON = JsonConvert.SerializeObject(userCredentials)
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(userCredentialsJSON)
Try
Dim url = "http://192.168.2.252:7080/SCTraker_Servidor/api/Session"
Dim httpWebRequest = WebRequest.Create(url)
httpWebRequest.Method = "POST"
httpWebRequest.ContentLength = byteArray.Length
httpWebRequest.ContentType = "application/x-www-form-urlencoded"
Dim writer As New StreamWriter(httpWebRequest.GetRequestStream(), System.Text.Encoding.UTF8)
writer.Write(userCredentialsJSON)
**writer.Close()**
Dim dataStream = httpWebRequest.GetResponse.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim userJSON = reader.ReadLine
Dim user = Linq.JObject.Parse(userJSON)
VariablesGlobales.usuario = user
If user IsNot "" Then
Me.Hide()
AJAXJSON.Show()
Else
BarraEstado.Text = "No ha rellenado el campo usuario o contraseña"
End If
Catch webException As WebException
Dim status = webException.Status
BarraEstado.Text = status + ":" + webException.Message
Catch socketException As Sockets.SocketException
BarraEstado.Text = socketException.Message
End Try
End If
End Sub
这样,服务器会在“** **”行中返回一个套接字异常,并显示“现有连接被远程主机强制关闭”错误代码:10054
最佳答案
我认为这不是你的全部问题,但我不能否认成功!
You are currently telling the server you are sending application/x-www-form-urlencoded data. Which you are not, as far as I can tell, and that is probably why the server is disconnecting you.
欢迎来到 Stack Overflow!
关于java - 如何使用 Visual Basic 向需要字符串的服务器发出 POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26356788/