Search code examples
jsonpostwebclientuploadstring

POST using the uploadstring method to call a web service and pass a json array


I am attempting to do a POST to a web service. I am using the WebClient class and calling the uploadstring method. This works fine until the web service I call expects some data, particularly a json array. I am trying to find out what format the data needs to be in, in order for the web service to accept and consume it properly. Example:

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

Any help here would be appreciated!

the web service (vb.net) being called takes a 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

Solution

  • I found a solution for this. The data has to be in json format literally:

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

    I created a class serialized it and then finagled the square brackets in.

    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, "]")