我正在尝试调用休息服务..该服务返回 json 对象...我能够发送请求但无法序列化 json...这是我正在尝试的代码
Function Sendingrequest(Item As Object) As String
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim res As StreamReader
Try
request = DirectCast(WebRequest.Create("http://localhost/SomeService/Type?id=" Item), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
res = New StreamReader(response.GetResponseStream())
Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(res.[GetType]())
Dim ms As New MemoryStream()
serializer.WriteObject(ms, res)
Dim json As String = Encoding.[Default].GetString(ms.ToArray())
Return json
Finally
If Not response Is Nothing Then response.Close()
End Try
End Function
我不确定我是否通过 url 正确传递参数...我将 url 硬编码为“http://localhost/SomeService/Type?id=ABC”然后 url 被命中但我收到此错误
不应使用数据合同名称“UTF8Encoding.UTF8Decoder:http://schemas.datacontract.org/2004/07/System.Text”键入“System.Text.UTF8Encoding+UTF8Decoder”。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。
有什么建议么?
谢谢你。