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