Uploading POST Data to PHP in Visual Basic.NET

Introduction: This tutorial is on how to upload POST data to a PHP file on a web server or localhost through Visual Basic .NET. Why POST? POST is the method used to upload sensitive data such as passwords, as opposed to using the PHP GET method which will simply show all data in the URL bar; GET - http://www.website.com/page.php?password=ThisIsMyGETPassword POST - http://www.website.com/page.php Both example sites would receive the same information, but the POST one is undercover and therefore slightly more secure. Why Only Slightly More Secure? Through the use of tools such as packet sniffers (WireShark), attackers can intercept any TCP/UDP/HTTP (etc.) packets that are sent over a network, these packets include data going to and from one location to another, in our case it would be our app sending the information to our web server PHP page. Once the packet is caught by the attacker, the sensitive information would be uncovered. This can be avoided through the use of Cryptography which I will be talking about in a near future tutorial. Visual Basic: So to send the data from our vb .NET application to our web server, we first need; The data to send. The sending method. The receiving location. So first lets get the data we want to send, I am going to send a basic message with the key of 'message' and value of 'hello', we will then output the value 'hello' through the use of the POST data key 'message' later on in our PHP file once the data has been sent/uploaded. All data sent through PHP (at least, using this method) must be a byte array, we can convert our string 'hello' to bytes through the Encoding namespace;
  1. Dim postData As Byte() = Encoding.Default.GetBytes("hello")
Next we want to send our postData as a byte array to our 'sendPost' function we are about to create...
  1. sendPost(postData)
Next we want to create the outline for our 'sendPost' function...
  1. Private Function sendPost(ByVal p As Byte()) As String
  2. Dim encoding As New UTF8Encoding
  3. Dim byteData As Byte() = p
  4. End Function
These are basic variables we will use in a minute. We don't need to redefine the byteData, but it will make it look less complicated later. Now we want to create a HTTPWebRequest to our server location...
  1. Private Function sendPost(ByVal p As Byte()) As String
  2. Dim encoding As New UTF8Encoding
  3. Dim byteData As Byte() = p
  4. Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://www.website.com/upload.php"), HttpWebRequest)
  5. postReq.Method = "POST"
  6. postReq.KeepAlive = True
  7. postReq.ContentType = "application/x-www-form-urlencoded"
  8. postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
  9. postReq.ContentLength = byteData.Length
  10. End Function
Now we have a web request going to our server location with the page of 'upload.php', the request also has some basic properties set. The user agent is set as the Chrome User Agent String (Google this if you need to, or for other browser simulation), the contentLength and Method properties are important to POST uploading. Finally we want to read the response. This just seals the deal. I also read my response and send it back to the function call, just in case something goes wrong - that way, I can add debugging to the PHP file, and visual basic project to see exactly what is going wrong, if it does.
  1. Private Function sendPost(ByVal p As Byte()) As String
  2. Dim encoding As New UTF8Encoding
  3. Dim byteData As Byte() = p
  4. Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36"), HttpWebRequest)
  5. postReq.Method = "POST"
  6. postReq.KeepAlive = True
  7. postReq.ContentType = "application/x-www-form-urlencoded"
  8. postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
  9. postReq.ContentLength = byteData.Length
  10. Dim postreqstream As Stream = postReq.GetRequestStream()
  11. postreqstream.Write(byteData, 0, byteData.Length)
  12. postreqstream.Close()
  13. Dim postresponse As HttpWebResponse
  14. postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
  15. Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
  16. Return postreqreader.ReadToEnd()
  17. End Function
PHP: For our PHP file 'upload.php', it is simple. We first check if the POST data is set, remember my earlier explanation that my key is 'message' while it's value is 'hello'. We check for the key, and NOT the value. We do this like so;
  1. <?php
  2. if (isSet($_POST['message'])) {
  3.  
  4. }
  5. ?>
We can then output appropriate text in to our page, like so...
  1. <?php
  2. if (isSet($_POST['message'])) {
  3. echo $_POST['message']; //Should be 'hello'
  4. }else
  5. echo 'No key with the method of POST (key, "message") was found!';
  6. ?>
REMEMBER; our application will output everything that the PHP page says, so if it doesn't work, we can add more output and see exactly what is wrong; Does the key exist? Do any POST parameters exist? Is the value of the key correct? If POST data does exist but it isn't our 'message' key, what keys and values are there? Finished!

Comments

Submitted byDalan (not verified)on Thu, 12/01/2016 - 23:57

Hello sir I have one error like this. Please if you can help sen me email to [email protected] Thanks

Add new comment