Tutorial [C#] Upload to pastebin
#1
I made this some time ago, it basicly uploads the code to www.pastebin.com
Note that this can be done with alot of websites but the variables just have a different name
SIMPLE VERSION:
This one just uploads the file and dat's it.

First make a simple form with a few textboxes and a button:
[Image: muniuTEcId.png]

then double click the button. All we do is send an array to a website and wait for the response. Here are the variables that you define:

Code:
System.Collections.Specialized.NameValueCollection Data = new System.Collections.Specialized.NameValueCollection(); Data["api_paste_name"] = textBox2.Text; Data["api_paste_expire_date"] = "N"; Data["api_paste_code"] = textBox1.Text; Data["api_dev_key"] = "YOUR DEV KEY HERE"; Data["api_option"] = "paste";

you devkey can be found here (you need a pastebin account): http://pastebin.com/api#1

textbox1.text will be the text that gets uploaded and textbox2.text is the title of the paste. The expiration codes can be found here: http://pastebin.com/api#6 the current one doesn't expire ("N" .. Never). Don't change the option: "paste".

now we gotta send that shit to www.pastebin.com, for this we use a WebClient (its in the System.Net namespace)

Code:
WebClient wb = new WebClient(); byte[] bytes = wb.UploadValues( "http://pastebin.com/api/api_post.php", Data );

now "bytes" will contain a byte array which we need to convert to a string:
Code:
string response; using ( MemoryStream ms = new MemoryStream( bytes ) ) using ( StreamReader reader = new StreamReader( ms ) ) response = reader.ReadToEnd();

now "response" will contains either an error or the link to the paste, to check if its an error just use:

Code:
if ( response.StartsWith( "Bad API request" ) ) { // do whatever you want when there's something wrong with the paste } else { // do the stuff if the paste is correct textBox3.Text = response; }

And there you go, now you can check the link and see if its working!

Here's my end result:
Code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Forms; using System.Net; using System.IO; namespace Pastebin { public partial class Form1 : Form { public Form1() { InitializeComponent(); textBox3.Enabled = false; textBox3.ReadOnly = true; } private void button1_Click( object sender, EventArgs e ) { Upload: System.Collections.Specialized.NameValueCollection Data = new System.Collections.Specialized.NameValueCollection(); Data["api_paste_name"] = textBox2.Text; Data["api_paste_expire_date"] = "N"; Data["api_paste_code"] = textBox1.Text; Data["api_dev_key"] = "4dd29af09faf6e39abcae2c910ad8f2e"; Data["api_option"] = "paste"; WebClient wb = new WebClient(); byte[] bytes = wb.UploadValues( "http://pastebin.com/api/api_post.php", Data ); string response; using ( MemoryStream ms = new MemoryStream( bytes ) ) using ( StreamReader reader = new StreamReader( ms ) ) response = reader.ReadToEnd(); if ( response.StartsWith( "Bad API request" ) ) { if ( MessageBox.Show( "Failed to upload!", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error ) == DialogResult.Retry ) goto Upload; } else { if ( checkBox1.Checked ) response = "http://pastebin.com/raw.php?i=" + response.Substring( 20 ); Clipboard.SetText( response ); textBox3.Text = response; textBox3.Enabled = true; MessageBox.Show( "Succesfully uploaded! \r\nLink copied to clipboard.", "Success!" ); } } } }
I added a "Raw file" button so it shows the raw file .. kinda pointless but whatever Big Grin

ADVANCED VERSION:
this version can upload a paste to an account, make it private and format it.

so we just copy and paste the stuff we have from the simple version, add 2 extra textboxes to the form which will be the username and password and we add a combobox for private/public.

Note: I replaced the bytes to text part with this function:
Code:
string ResponseText( byte[] bytes ) { using ( MemoryStream ms = new MemoryStream( bytes ) ) using ( StreamReader reader = new StreamReader( ms ) ) return reader.ReadToEnd(); }

First the login part, we need to get a "api_user_key" which we can get the same way as posting it. We need 3 things:
Code:
Data["api_user_name"] = textBox4.Text; Data["api_user_password"] = textBox5.Text; Data["api_dev_key"] = "YOUR DEV KEY";

then we send it again and wait for the response:
Code:
bytes = wb.UploadValues( "http://pastebin.com/api/api_login.php", Data ); response = ResponseText( bytes );

again check for errors:
Code:
if ( response.StartsWith( "Bad API request" ) )

if it doesn't return an error then:
Code:
Data["api_user_key"] = response;

now when you send the data it will be uploaded to that account Smile

for formatted code we just add:
Code:
Data["api_paste_format"] = "FORMAT CODE HERE";
codes can be found here: http://pastebin.com/api#5

And the last one: Private/public pastes which is also pretty easy:
Code:
Data["api_paste_private"] = comboBox2.SelectedItem.ToString().ToLower() == "private" ? "1" : "0";
For this you need 2 options in your combobox, "Private" and "Public". Smile

edit:
Here's the endresult for the advanced one:
Code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Forms; using System.Net; using System.IO; namespace Pastebin { public partial class Form1 : Form { public Form1() { InitializeComponent(); textBox3.Enabled = false; textBox3.ReadOnly = true; } string ResponseText( byte[] bytes ) { using ( MemoryStream ms = new MemoryStream( bytes ) ) using ( StreamReader reader = new StreamReader( ms ) ) return reader.ReadToEnd(); } private void button1_Click( object sender, EventArgs e ) { Upload: System.Collections.Specialized.NameValueCollection Data = new System.Collections.Specialized.NameValueCollection(); WebClient wb = new WebClient(); string response; byte[] bytes; if ( textBox4.Text != "Username" && textBox5.Text != "Password" ) { Data["api_user_name"] = textBox4.Text; Data["api_user_password"] = textBox5.Text; Data["api_dev_key"] = "4dd29af09faf6e39abcae2c910ad8f2e"; bytes = wb.UploadValues( "http://pastebin.com/api/api_login.php", Data ); response = ResponseText( bytes ); if ( response.StartsWith( "Bad API request" ) ) { if ( MessageBox.Show( "Login failed, do you want to upload it as guest?", "Login failed", MessageBoxButtons.YesNo, MessageBoxIcon.Error ) == DialogResult.Yes ) { Data["api_user_name"] = textBox4.Text; Data["api_user_password"] = textBox5.Text; goto SkipLogin; } else return; } Data["api_user_key"] = response; } SkipLogin: Data["api_paste_private"] = comboBox2.SelectedItem.ToString().ToLower() == "private" ? "1" : "0"; Data["api_paste_name"] = textBox2.Text; Data["api_paste_expire_date"] = "N"; Data["api_paste_code"] = textBox1.Text; Data["api_option"] = "paste"; bytes = wb.UploadValues( "http://pastebin.com/api/api_post.php", Data ); response = ResponseText( bytes ); if ( response.StartsWith( "Bad API request" ) ) { if ( MessageBox.Show( "Failed to upload!\r\n" + response, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error ) == DialogResult.Retry ) goto Upload; } else { if ( checkBox1.Checked ) response = "http://pastebin.com/raw.php?i=" + response.Substring( 20 ); Clipboard.SetText( response ); textBox3.Text = response; textBox3.Enabled = true; MessageBox.Show( "Succesfully uploaded! \r\nLink copied to clipboard.", "Success!" ); } } } }
(08-10-2011, 12:58)Pozzuh Wrote:
Se7en Wrote:Stealed, from cod4 mod ...
look who's talking

[Release] Old School Mod v2.2
[Release] Scroll menu

Reply

#2
Nice tutorial
[Image: MaEIQ.png]
Reply

#3
C# tutorials spam attack
Reply

#4
(02-01-2012, 16:18)kokole Wrote: C# tutorials spam attack

Yeah thats why I wanted to post this aswell :p We need more of these small things
(08-10-2011, 12:58)Pozzuh Wrote:
Se7en Wrote:Stealed, from cod4 mod ...
look who's talking

[Release] Old School Mod v2.2
[Release] Scroll menu

Reply

#5
Well with all this small tutos you can make something big!

Keep em COMMING! Troll

Thanks Barata...
Don't worry if things aren't the way you planned, in the end everything will solve itself...
Reply

#6
I've brought C/C++ section back to life Smile

good tutorial
[Image: lQDUjba.jpg]
Reply

#7
Nice work, keep it up !
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Upload Clipboard Text data to a server DidUknowiPwn 14 11,189 04-13-2013, 14:48
Last Post: SuperNovaAO
  Help C# FTP upload image problem narkos 3 7,670 11-27-2012, 19:15
Last Post: narkos
  [Request] Can someone upload a 'iw_06' from there MAIN folder please. b0b722 5 5,654 07-25-2012, 22:56
Last Post: Arteq
  [Request] Patch_mp.ff upload KamikazeOnCrack 2 3,055 12-06-2011, 08:51
Last Post: d0h!
  Black Ops Mod upload tutorial shown with xkmod mac1981 22 29,536 09-04-2011, 13:16
Last Post: Fredju87d
  Help Upload mod, server dont start Puffiamo 4 4,549 07-27-2011, 15:48
Last Post: d0h!

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2026 Melroy van den Berg.