我正在尝试将POST请求发送到shapeshift,该请求几乎没有要作为JSON发送的参数,然后希望在VB.NET中显示部分响应
变形的文档:https://info.shapeshift.io/api#api-9
以下是我到目前为止尝试过的内容:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim request As HttpWebRequest
Dim response As HttpWebResponse
Dim reader As StreamReader
Dim rawresponse As String
Try
request = DirectCast(WebRequest.Create("https://shapeshift.io/sendamount"), HttpWebRequest)
request.ContentType = "application/json"
request.Method = "POST"
Dim postdata As String = "{""amount"":}" + TextBox1.Text + "{,""withdrawal"":}" + TextBox2.Text + "{,""pair"":""btc_eth""}" + "{,""returnAddress"":}" + TextBox3.Text
request.ContentLength = postdata.Length
Dim requestWriter As StreamWriter = New StreamWriter(request.GetRequestStream())
requestWriter.Write(postdata)
requestWriter.Close()
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
rawresponse = reader.ReadToEnd()
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
Dim json As String = rawresponse
Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(json)
Label1.Text = jsonObject("expiration").ToString
End Sub
我得到的错误是:400错误的请求
我认为是因为我弄乱了解释JSON POST请求的代码中的某些内容。我做了很多研究,尝试了几件事,但没有任何效果:(
答案 0 :(得分:1)
尝试以下方法:
Dim postdata As String = "{""amount"":" + TextBox1.Text + "},{""withdrawal"":""" + TextBox2.Text + """},{""pair"":""btc_eth""},{""returnAddress"":""" + TextBox3.Text + """}"
您的数据格式不正确。
或者这是String.Format的另一个版本:
Dim postdata2 As String = String.Format("{{""amount"":{0}}},{{""withdrawal"":""{1}""}},{{""pair"":""btc_eth""}},{{""returnAddress"":""{2}""}}", TextBox1.Text, TextBox2.Text, TextBox3.Text)
答案 1 :(得分:0)
WebRequest现在已经很老了。您可能要使用较新的System.Net.Http.HttpClient,官方文档为here。
此外,在转换为Json时,我强烈建议使用带有通用参数(Of ...)的Newtonsoft.Json nuget包的JConvert / Deserialize功能将其转换为预定义的对象。在返回时节省了大量的手动文本解析。
模拟了一个简单的示例:
Private async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim transaction As New MyRequestData With
{
.Amount = Convert.ToDecimal(TextBox1.Text),
.Withdrawal = Convert.ToDecimal(TextBox2.Text),
.ReturnAddress = TextBox3.Text
}
Dim content = newtonsoft.Json.JsonConvert.SerializeObject(transaction)
dim buffer = System.Text.Encoding.UTF8.GetBytes(content)
Dim bytes = new Net.Http.ByteArrayContent(buffer)
bytes.Headers.ContentType = new Net.Http.Headers.MediaTypeHeaderValue("application/json")
Dim responseBody As string = nothing
Using client As New System.Net.Http.HttpClient
Dim response = Await client.PostAsync("https://shapeshift.io/sendamount",bytes)
responsebody = Await response.Content.ReadAsStringAsync()
End Using
Dim data = Newtonsoft.Json.JsonConvert.DeserializeObject(Of MyResponseData)(responsebody)
If data.Expiration Is Nothing
Label1.Text = data.Error
Else
Label1.Text = data.Expiration
End If
End Sub
Public class MyRequestData
Public property Amount As Decimal
Public property Withdrawal As Decimal
Public property Pair As String = "btc_eth"
Public property ReturnAddress As String
End Class
Public class MyResponseData
Public property Expiration As String
Public property [Error] As String
End Class
希望这会有所帮助,祝您好运!