2013-03-07 120 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, "]") 
相关问题