title: POST variables to web server?
body: <p>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.</p>
  <p>Does someone know the correct way to use a <code>WebClient</code> to POST data? I'd rather use <code>WebClient</code> because it requires less code than <code>HttpWebRequest</code>/<code>HttpWebResponse</code>.</p>
  <p>Here's what I tried so far:</p>
  <pre><code>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(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
      Dim postData = String.Format(&quot;db={0}&amp;query={1}&quot;, _
                                   HttpUtility.UrlEncode(&quot;books.sqlite&quot;), _
                                   HttpUtility.UrlEncode(&quot;SELECT id,title FROM boooks&quot;))
  
      ''#Dim bytArguments As Byte() = Encoding.ASCII.GetBytes(&quot;db=books.sqlite|query=SELECT * FROM books&quot;)
  
      ''#POST query
      Dim bytRetData As Byte() = wc.UploadData(&quot;http://localhost:9999/get&quot;, &quot;POST&quot;, postData)
      RichTextBox1.Text = Encoding.ASCII.GetString(bytRetData)
  
      Exit Sub
  
      Dim client = New WebClient()
  
      Dim nv As New Collection
      nv.Add(&quot;db&quot;, &quot;books.sqlite&quot;)
      nv.Add(&quot;query&quot;, &quot;SELECT id,title FROM books&quot;)
  
      Dim address As New Uri(&quot;http://localhost:9999/get&quot;)
  
      ''#Dim bytRetData As Byte() = client.UploadValues(address, &quot;POST&quot;, nv)
  
      RichTextBox1.Text = Encoding.ASCII.GetString(bytRetData)
  
      Exit Sub
  
      ''#Dim wc As New WebClient()
  
      ''#convert data
      wc.Headers.Add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
      Dim bytArguments As Byte() = Encoding.ASCII.GetBytes(&quot;db=books.sqlite|query=SELECT * FROM books&quot;)
  
      ''#POST query
      ''#Dim bytRetData As Byte() = wc.UploadData(&quot;http://localhost:9999/get&quot;, &quot;POST&quot;, bytArguments)
      RichTextBox1.Text = Encoding.ASCII.GetString(bytRetData)
  
      Exit Sub
  End Sub
  </code></pre>
  <p>Thank you.</p>
  <hr />
  <p>Edit: Thanks everyone for the feedback.</p>
  <p>For those having the same problem, here's some working code:</p>
  <pre><code>Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim baseURL As String = &quot;localhost:9999&quot;
      Dim strURL As String = &quot;http://&quot; &amp; baseURL &amp; &quot;/get&quot;
      
      Dim client As New WebClient()
      
      client.Headers.Add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
      client.Headers.Add(&quot;Accept-Encoding&quot;, &quot;text/plain&quot;)
      client.Headers.Add(&quot;User-Agent&quot;, &quot;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&quot;)
      'BAD client.Headers.Add(&quot;Host&quot;, baseURL)
      'BAD client.Headers.Add(&quot;Host&quot;, &quot;localhost&quot;)
      
      'Convert in parameters in case contain spaces, accents, etc.
      Dim postData As String = String.Format(&quot;db={0}&amp;query={1}&quot;, _
                           HttpUtility.UrlEncode(&quot;books.sqlite&quot;), _
                           HttpUtility.UrlEncode(&quot;SELECT id,title FROM books&quot;))
      
      RichTextBox1.Text = client.UploadString(New Uri(strURL), &quot;POST&quot;, postData)
  End Sub
  </code></pre>