views:

146

answers:

1

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.


Edit: Thanks everyone for the feedback.

For those having the same problem, here's some working code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim baseURL As String = "localhost:9999"
    Dim strURL As String = "http://" & baseURL & "/get"

    Dim client As New WebClient()

    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
    client.Headers.Add("Accept-Encoding", "text/plain")
    client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Iron/4.0.280.0 Chrome/4.0.280.0 Safari/532.9")
    'BAD client.Headers.Add("Host", baseURL)
    'BAD client.Headers.Add("Host", "localhost")

    'Convert in parameters in case contain spaces, accents, etc.
    Dim postData As String = String.Format("db={0}&query={1}", _
                         HttpUtility.UrlEncode("books.sqlite"), _
                         HttpUtility.UrlEncode("SELECT id,title FROM books"))

    RichTextBox1.Text = client.UploadString(New Uri(strURL), "POST", postData)
End Sub
+3  A: 

Since you're using "localhost" as the target for your web client I'll assume you control this site as well. Accepting SQL through the query string for that site is an exceptionally bad idea.

The answer to your question, then, is to re-write your web site so that it expects only the parameters to plug into the query rather than the query itself.

Some other problems with your code:

  • WebClient implements IDisposable, and so should be created with a using block
  • I think UploadData() is probably the wrong function to call - you expect a response from the server, don't you? Try DownloadString() instead; it will still post your data.
  • I'll give you the benefit of the doubt and assume the "Exit Sub" lines are there for debugging, but it's worth mentioning that breakpoints would serve you better.
Joel Coehoorn
Thanks for the tip on IDisposable. This code is just for learning purposes, and the actual SQL query will be generated by the server from parameters sent by the client.
OverTheRainbow