Advertisement
andrew4582

Pastebin API

Mar 4th, 2011
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. string pastebin_login_url = "http://pastebin.com/api/api_login.php";
  2.         string pastebin_post_url = "http://pastebin.com/api/api_post.php";
  3.         string pastebin_dev_key = "???????????????????????????";
  4.         string _pastebinUserKey = null;
  5.  
  6.         void SendViaPasteBin() {
  7.  
  8.             string body = GetText(txtMain);
  9.  
  10.             string subject = txtSubject.Text.Trim();
  11.             if(string.IsNullOrEmpty(subject)) {
  12.                 subject = "Note " + DateTime.Now.ToLongDateString() + ":" + DateTime.Now.ToShortTimeString();
  13.             }
  14.  
  15.             string api_paste_format = "csharp";
  16.             string userk = LogInToPasteBin();
  17.  
  18.             NameValueCollection values = new NameValueCollection();
  19.             values.Add("api_dev_key",pastebin_dev_key);
  20.             values.Add("api_option","paste");
  21.             values.Add("api_paste_code",body);
  22.             values.Add("api_paste_private","0");
  23.             values.Add("api_paste_name",subject);
  24.             values.Add("api_paste_expire_date","N");
  25.             values.Add("api_paste_format",api_paste_format);
  26.             values.Add("api_user_key",userk);
  27.  
  28.             using(WebClient wc = new WebClient()) {
  29.  
  30.                 byte[] respBytes = wc.UploadValues(pastebin_post_url,values);
  31.                 string respString = Encoding.UTF8.GetString(respBytes);
  32.  
  33.                 Uri valid = null;
  34.                 if(Uri.TryCreate(respString,UriKind.Absolute,out valid)) {
  35.                     MessageBox.Show("Success: " + valid.ToString());
  36.                 }
  37.                 else {
  38.                     MessageBox.Show("Error:" + respString);
  39.                 }
  40.             }
  41.         }
  42.  
  43.         string LogInToPasteBin() {
  44.  
  45.             if(_pastebinUserKey != null)
  46.                 return _pastebinUserKey;
  47.  
  48.             string userName = "?????????";
  49.             string pwd = "?????????";
  50.  
  51.             NameValueCollection values = new NameValueCollection();
  52.             values.Add("api_dev_key",pastebin_dev_key);
  53.             values.Add("api_user_name",userName);
  54.             values.Add("api_user_password",pwd);
  55.  
  56.             using(WebClient wc = new WebClient()) {
  57.                 byte[] respBytes = wc.UploadValues(pastebin_login_url,values);
  58.                 string resp = Encoding.UTF8.GetString(respBytes);
  59.                 if(resp.Contains("Bad API request")) {
  60.                     MessageBox.Show("Error:" + resp);
  61.                     return null;
  62.                 }
  63.                 _pastebinUserKey = resp;
  64.             }
  65.             return _pastebinUserKey;
  66.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement