P0ST variables to web server?

Posted by OverTheRainbow on Stack Overflow See other posts from Stack Overflow or by OverTheRainbow
Published on 2010-05-03T18:53:52Z Indexed on 2010/05/03 18:58 UTC
Read the original article Hit count: 278

Filed under:
|
|

Hello

I've been trying several things from Google to POST data to a web server, but none of them work: I'm still stuck at how to convert the variables into the request, considering that the second variable is an SQL query so it has spaces.

Does someone know the correct way to use a WebClient to POST data? I'd rather use WebClient because it requires less code than HttpWebRequest/HttpWebResponse.

Here's what I tried so far:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim wc = New WebClient()

    'convert data
    wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
    Dim postData = String.Format("db={0}&query={1}", _
                                 HttpUtility.UrlEncode("books.sqlite"), _
                                 HttpUtility.UrlEncode("SELECT id,title FROM boooks"))

    'Dim bytArguments As Byte() = Encoding.ASCII.GetBytes("db=books.sqlite|query=SELECT * FROM books")

    'POST query
    Dim bytRetData As Byte() = wc.UploadData("http://localhost:9999/get", "POST", postData)
    RichTextBox1.Text = Encoding.ASCII.GetString(bytRetData)

    Exit Sub

    Dim client = New WebClient()

    Dim nv As New Collection
    nv.Add("db", "books.sqlite")
    nv.Add("query", "SELECT id,title FROM books")

    Dim address As New Uri("http://localhost:9999/get")

    'Dim bytRetData As Byte() = client.UploadValues(address, "POST", nv)

    RichTextBox1.Text = Encoding.ASCII.GetString(bytRetData)

    Exit Sub

    'Dim wc As New WebClient()

    'convert data
    wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
    Dim bytArguments As Byte() = Encoding.ASCII.GetBytes("db=books.sqlite|query=SELECT * FROM books")

    'POST query
    'Dim bytRetData As Byte() = wc.UploadData("http://localhost:9999/get", "POST", bytArguments)
    RichTextBox1.Text = Encoding.ASCII.GetString(bytRetData)

    Exit Sub
End Sub

Thank you.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about webforms