【发布时间】:2020-07-16 09:12:43
【问题描述】:
我正在尝试在 vb.net 中调用 openrouteservice api,因此尝试关注 documentation/examples。该示例显示以下代码作为工作示例:
Sub Main(args As String())
Dim request = TryCast(System.Net.WebRequest.Create("https://api.openrouteservice.org/v2/directions/driving-car/gpx"), System.Net.HttpWebRequest)
request.Method = "POST"
request.Accept = "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8"
request.Headers.Add("Authorization", "xxxxMYKEYxxxx")
request.Headers.Add("Content-Type", "application/json; charset=utf-8")
Using writer = New System.IO.StreamWriter(request.GetRequestStream())
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes({"coordinates:[[8.681495,49.41461],[8.686507,49.41943],[8.687872,49.420318]]"})
request.ContentLength = byteArray.Length
writer.Write(byteArray)
writer.Close()
End Using
Dim responseContent As String
Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
Using reader = New System.IO.StreamReader(response.GetResponseStream())
responseContent = reader.ReadToEnd()
Console.WriteLine(responseContent.ToString())
End Using
End Using
End Sub
示例显示请求正文的内容应该如下:
{"coordinates":[[8.681495,49.41461],[8.686507,49.41943],[8.687872,49.420318]]}
但是在运行示例时出现以下错误:
'The remote server returned an error: (500) Internal Server Error.'
所以我认为请求失败是因为 Json 无效。 我的问题是如何创建一个与上面提供的示例 JSON 相同的 JSON?
【问题讨论】:
标签: json vb.net rest httpwebrequest