Uploading POST Data to PHP in C#
Submitted by Yorkiebar on Wednesday, July 16, 2014 - 05:28.
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;
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...
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'...
We could create multiple POST data kvps by adding multiple lines using commas, like so...
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.
We write the results to the console just for testing purposes so we can see what is happening.
Finished!
- using System;
- using System.Net;
- using System.Collections.Specialized;
- using System.Runtime.InteropServices;
- namespace PostDataProgram
- {
- class Program
- {
- static void Main(string[] args)
- {
- }
- }
- }
- using System;
- using System.Net;
- using System.Collections.Specialized;
- using System.Runtime.InteropServices;
- namespace BotnetKL
- {
- class Program
- {
- static void Main(string[] args)
- {
- sendIt();
- }
- static void sendIt()
- {
- }
- }
- }
- using System;
- using System.Net;
- using System.Collections.Specialized;
- using System.Runtime.InteropServices;
- namespace BotnetKL
- {
- class Program
- {
- static void Main(string[] args)
- {
- sendIt();
- }
- static void sendIt()
- {
- {
- {"klData", "data"}
- };
- }
- }
- }
- using System;
- using System.Net;
- using System.Collections.Specialized;
- using System.Runtime.InteropServices;
- namespace BotnetKL
- {
- class Program
- {
- static void Main(string[] args)
- {
- sendIt();
- }
- static void sendIt()
- {
- {
- {"klData", "data"},
- {"klData2", "moreData"}
- };
- }
- }
- }
- using System;
- using System.Net;
- using System.Collections.Specialized;
- using System.Runtime.InteropServices;
- namespace BotnetKL
- {
- class Program
- {
- static void Main(string[] args)
- {
- sendIt();
- }
- static void sendIt()
- {
- {
- {"klData", "data"}
- };
- try
- {
- var s = wc.UploadValues("http://example.com/kl.php", postData);
- Console.WriteLine(s);
- }
- catch (Exception ex) { Console.WriteLine(ex.ToString()); }
- }
- }
- }
Add new comment
- 106 views