Link to home
Start Free TrialLog in
Avatar of eeyo
eeyo

asked on

How do I get the content from an HTTPclient POST in VB.NET?

How do I get the content from an HTTPclient POST in VB.NET?

Here is what I have tried so for in my Winform Application.  In this example, I am trying to send 2 values, and I want to send the result to "responseString".
        Dim client As HttpClient = New HttpClient

        Dim values As New Dictionary(Of String, String) From {{"Input1", "hello"}, {"Input2", "world"}}
        Dim content As Object = New FormUrlEncodedContent(values)
        Dim response As Task(Of HttpResponseMessage) = client.PostAsync("http://www.website.com/webform.aspx", content)
        Dim responseString As String = response.Content.ReadAsStringAsync
        MessageBox.Show(responseString)

Open in new window

I am currently getting my error at "response.content", probably becuase I am not sure how to handle a "Task(Of HttpResponseMessage)":
Dim responseString As String = response.Content.ReadAsStringAsync

Open in new window


BTW, the above code is what I cobbled together from websearches, but if I the amount of data expected is relatively small, perhaps it would be easier to come with a solution that isn't asynchronous?
Avatar of eeyo
eeyo

ASKER

I have also tried webclient as well, but no luck.  Here is the website form:
    <input name="Input1" type="text" id="MainContent_txtInput1" />
    <input name="Input2 type="text" id="MainContent_txtInput2 " />
    <input type="submit" name="ctl00$MainContent$Submit" value="Button" id="MainContent_Submit" />
    <span id="MainContent_lblNumber">Label</span>

Open in new window

Here is the vb.net code.  The responseInString text is the same.  Doesn't seem to be changed by the input1 or input2 values.
        Dim wb = New WebClient
        Dim data As New NameValueCollection()
        data("Input1") = "hello"
        data("Input2") = "world"
        Dim response = wb.UploadValues("http://localhost:64641/Default", "POST", data)
        Dim responseInString As String = Encoding.UTF8.GetString(response)
        txtResult.Text = responseInString

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of eeyo
eeyo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial