我需要通过发送Json请求来连接服务器。
服务器管理员告诉我应该安装https服务器。
我买了一个ssl并将其安装在我的服务器上。
他们向我发送了json字符串格式,即如何从他们的服务器获取数据。 我尝试发出一个示例请求,但每次收到此错误时:
远程服务器返回错误:(400)错误请求。
我打电话给他们,他们说请求缺少私钥或公钥证书(我发给他们证书,他们也把它安装在他们的服务器上)但仍然得到同样的错误。
这是我使用的代码:
有人可以告诉我,如果我有什么问题吗?
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
POST("https://gw.bisan.com/api/apdemo_6", "{""user"":""MyUser"",""password"":""MyPASS"",""command"":""table"",""table"":""currency"",""fields"":[""symbol"",""rate""]}")
' Read()
End Sub
Private Sub POST(url As String, jsonContent As String)
Try
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
request.ContentType = "text/json"
request.Method = "POST"
Dim streamWriter As StreamWriter = New StreamWriter(request.GetRequestStream())
streamWriter.Write(jsonContent)
streamWriter.Flush()
streamWriter.Close()
Dim httpResponse As HttpWebResponse = request.GetResponse()
Dim streamReader As StreamReader = New StreamReader(httpResponse.GetResponseStream())
Dim result = streamReader.ReadToEnd()
Label1.Text = result.ToString
Catch ex As WebException
Label1.Text = ex.Message
End Try
End Sub
答案 0 :(得分:1)
在您的开发计算机http://www.telerik.com/fiddler上安装Fiddler,启动它并从您的计算机发出请求。 Fiddler将捕获所有发布请求信息并为您组织,以便您可以准确查看代码发送的内容并将其与服务器期望的内容进行比较。从那里你应该能够更容易地看到400错误原因并相应地修改你的代码。
我猜这个问题出在JSON字符串的最终输出或者request.ContentType =" text / json" (也许需要是application / json)。无论哪种方式,Fiddler都是一种更快捷的发现方式。
答案 1 :(得分:0)
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
POST("https://gw.bisan.com/api/apdemo_6", "{""user"":""MyUser"",""password"":""MyPASS"",""command"":""table"",""table"":""currency"",""fields"":[""symbol"",""rate""]}")
' Read()
End Sub
Private Sub POST(url As String, jsonContent As String)
Try
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
request.ContentType = "application/json"
request.Method = "POST"
Dim streamWriter As StreamWriter = New StreamWriter(request.GetRequestStream())
streamWriter.Write(jsonContent)
streamWriter.Flush()
streamWriter.Close()
Dim httpResponse As HttpWebResponse = request.GetResponse()
Dim streamReader As StreamReader = New StreamReader(httpResponse.GetResponseStream())
Dim result = streamReader.ReadToEnd()
Label1.Text = result.ToString
Catch ex As WebException
Label1.Text = ex.Message
End Try
End Sub