Uploading POST Data to PHP in C#

Introduction: This tutorial is on how to create POST data uploader in C#. POST or GET? If you don't know what the difference is between POST and GET, these are the methods used by PHP to gain information from external sources that parse the data to those PHP pages. GET is URL based such as; index.php?id=2 Id is 2. And POST is behind the scenes for sensitive information such as passwords. Project: So first we want to create a basic C# project through the File > New Project wizard. Here is the basic Visual Studio C# file;
  1. using System;
  2. using System.Net;
  3. using System.Collections.Specialized;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace PostDataProgram
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.            
  13.         }
  14.     }
  15. }
So now, in the main function, we are going to call another function named 'sendIt'. This will send the data, as well as call another function to create the data...
  1. using System;
  2. using System.Net;
  3. using System.Collections.Specialized;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace BotnetKL
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             sendIt();
  13.         }
  14.  
  15.         static void sendIt()
  16.         {
  17.         }
  18.     }
  19. }
This new sendIt function will first create the post data we want to send. In the following example, we create a new kvp (key value pair) of post data with the key named as 'klData', and value of 'data'...
  1. using System;
  2. using System.Net;
  3. using System.Collections.Specialized;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace BotnetKL
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             sendIt();
  13.         }
  14.  
  15.         static void sendIt()
  16.         {
  17.             var postData = new NameValueCollection()
  18.             {
  19.                 {"klData", "data"}
  20.             };
  21.         }
  22.     }
  23. }
We could create multiple POST data kvps by adding multiple lines using commas, like so...
  1. using System;
  2. using System.Net;
  3. using System.Collections.Specialized;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace BotnetKL
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             sendIt();
  13.         }
  14.  
  15.         static void sendIt()
  16.         {
  17.             var postData = new NameValueCollection()
  18.             {
  19.                 {"klData", "data"},
  20.                 {"klData2", "moreData"}
  21.             };
  22.         }
  23.     }
  24. }
Finally we want to create a new WebClient, and use the inbuilt 'uploadValues' function to upload the postData to our php file. The parameters of the function are; url to upload POST data to, postData to upload.
  1. using System;
  2. using System.Net;
  3. using System.Collections.Specialized;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace BotnetKL
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             sendIt();
  13.         }
  14.  
  15.         static void sendIt()
  16.         {
  17.             var postData = new NameValueCollection()
  18.             {
  19.                 {"klData", "data"}
  20.             };
  21.  
  22.             WebClient wc = new WebClient();
  23.             try
  24.             {
  25.                 var s = wc.UploadValues("http://example.com/kl.php", postData);
  26.                 Console.WriteLine(s);
  27.             }
  28.             catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  29.         }
  30.     }
  31. }
We write the results to the console just for testing purposes so we can see what is happening. Finished!

Add new comment