0

我正在尝试对 Web 服务进行 POST。我正在使用 WebClient 类并调用 uploadstring 方法。这工作正常,直到我调用的 Web 服务需要一些数据,尤其是 json 数组。我试图找出数据需要采用什么格式,以便 Web 服务能够正确接受和使用它。例子:

WebClient myWebClient = new WebClient();
string resp = myWebClient.UploadString("www.myUrl.com", "POST", "someDataToSend");

这里的任何帮助将不胜感激!

被调用的 Web 服务 (vb.net) 采用键值对:

<OperationContract(), WebInvoke(BodyStyle:=WebMessageBodyStyle.WrappedRequest, Method:="POST", RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)> _
Public Function DoSomething(ByVal myKeyValuePair() As KeyValuePair(Of String, String)) As String
4

1 回答 1

0

我找到了解决方案。数据必须是 json 格式:

"{"Type":[{"key":"cType","value":"Age"}]}"

我创建了一个对其进行序列化的类,然后将方括号放入其中。

Public Class cType
    Private _key As String
    Public Property Key() As String
        Get
            Return _key
        End Get
        Set(ByVal value As String)
            value = "cType"
            _key = value
        End Set
    End Property
    Public value As String
End Class 

Dim objType As cType = New cType
objType.value = "Age"
Dim myData As String = deserializer.Serialize(New With {.cType = objType})
myData = myData.Insert(12, "[")
myData = myData.Insert(myData.Length - 1, "]")
于 2013-03-07T20:25:03.997 回答