2013-03-07 121 views
0

我正在嘗試執行POST到Web服務。我正在使用WebClient類並調用uploadstring方法。這工作正常,直到我調用的Web服務需要一些數據,特別是一個JSON數組。我試圖找出數據需要採用什麼格式,以便Web服務能夠正確接受和使用它。例如:POST使用uploadstring方法調用Web服務並傳遞json數組

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

任何幫助在這裏將不勝感激!

Web服務(vb.net)被稱爲花費keyvaluepair:

<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 

回答

0

我找到了一個解決方案。該數據必須是JSON格式字面上:

「{」 類型 「:[{」 鍵 「:」 CTYPE 「 」值「: 」年齡「}]}」

我創建的類序列化它然後折成方括號。

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, "]") 
相關問題