VB.NET 2005 – WebClient.UploadString – POST Data Appearing Not To Be Sent

March 15th, 2008 by Oliver - Tagged with , - Posted in Web Development

(1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Once again ran into this issue while working on a small VB.NET project for a client.

Problem: When using the webclient to upload a string of data to a PHP/ASP/etc script with the following code (or similar), you aren’t able to pick the posted data up within your receiving script:

Dim wc As New WebClient
wc.UploadString(url, data)
I found a few articles resorting to other code (usually a lot more / complex�code, i.e. converting string to bytes and then using the UploadData method) to get this resolved.However the fix is simple. Tell the script what data your posting�by adding some headers!�Modify your code by adding this extra line inbetween defining the WebClient and using the UploadString method:
Dim wc As New WebClient
wc.Headers.Add(
“Content-Type”,“application/x-www-form-urlencoded”)
wc.UploadString(url, data)

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>