View difference between Paste ID: pzvNjarK and Dkg0esst
SHOW: | | - or go back to the newest paste.
1-
string pastebin_login_url = "http://pastebin.com/api/api_login.php";
1+
using System;
2-
        string pastebin_post_url = "http://pastebin.com/api/api_post.php";
2+
using System.Collections.Specialized;
3-
        string pastebin_dev_key = "???????????????????????????";
3+
using System.Net;
4-
        string _pastebinUserKey = null;
4+
using System.Text;
5
6-
        void SendViaPasteBin() {
6+
namespace PasteRight
7
{
8-
            string body = GetText(txtMain);
8+
    class PastebinSharp
9
    {
10-
            string subject = txtSubject.Text.Trim();
10+
        private string ILoginURL = "http://pastebin.com/api/api_login.php";
11-
            if(string.IsNullOrEmpty(subject)) {
11+
        private string IPostURL = "http://pastebin.com/api/api_post.php";
12-
                subject = "Note " + DateTime.Now.ToLongDateString() + ":" + DateTime.Now.ToShortTimeString();
12+
        private string IDevKey = "";
13
        private string IUserKey = null;
14
15-
            string api_paste_format = "csharp";
15+
        public void Send(string IBody, string ISubj, string IFormat)
16-
            string userk = LogInToPasteBin();
16+
        {
17
            if (string.IsNullOrEmpty(IBody.Trim())) { return; }
18-
            NameValueCollection values = new NameValueCollection();
18+
            if (string.IsNullOrEmpty(ISubj.Trim())) { return; }
19-
            values.Add("api_dev_key",pastebin_dev_key);
19+
20-
            values.Add("api_option","paste");
20+
            NameValueCollection IQuery = new NameValueCollection();
21-
            values.Add("api_paste_code",body);
21+
22-
            values.Add("api_paste_private","0");
22+
            IQuery.Add("api_dev_key", IDevKey);
23-
            values.Add("api_paste_name",subject);
23+
            IQuery.Add("api_option", "paste");
24-
            values.Add("api_paste_expire_date","N");
24+
            IQuery.Add("api_paste_code", IBody);
25-
            values.Add("api_paste_format",api_paste_format);
25+
            IQuery.Add("api_paste_private", "0");
26-
            values.Add("api_user_key",userk);
26+
            IQuery.Add("api_paste_name", ISubj);
27
            IQuery.Add("api_paste_expire_date", "N");
28-
            using(WebClient wc = new WebClient()) {
28+
            IQuery.Add("api_paste_format", IFormat);
29
            IQuery.Add("api_user_key", IUserKey);
30-
                byte[] respBytes = wc.UploadValues(pastebin_post_url,values);
30+
31-
                string respString = Encoding.UTF8.GetString(respBytes);
31+
            using (WebClient IClient = new WebClient())
32
            {
33-
                Uri valid = null;
33+
                string IResponse = Encoding.UTF8.GetString(IClient.UploadValues(IPostURL, IQuery));
34-
                if(Uri.TryCreate(respString,UriKind.Absolute,out valid)) {
34+
35-
                    MessageBox.Show("Success: " + valid.ToString());
35+
                Uri isValid = null;
36
                if (!Uri.TryCreate(IResponse, UriKind.Absolute, out isValid))
37-
                else {
37+
                {
38-
                    MessageBox.Show("Error:" + respString);
38+
                    throw new WebException("Paste Error", WebExceptionStatus.SendFailure);
39
                }
40
            }
41
        }
42
43-
        string LogInToPasteBin() {
43+
        public PastebinSharp(string Username, string Password)
44
        {
45-
            if(_pastebinUserKey != null)
45+
            NameValueCollection IQuery = new NameValueCollection();
46-
                return _pastebinUserKey;
46+
47
            IQuery.Add("api_dev_key", IDevKey);
48-
            string userName = "?????????";
48+
            IQuery.Add("api_user_name", Username);
49-
            string pwd = "?????????";
49+
            IQuery.Add("api_user_password", Password);
50
51-
            NameValueCollection values = new NameValueCollection();
51+
            using (WebClient wc = new WebClient())
52-
            values.Add("api_dev_key",pastebin_dev_key);
52+
            {
53-
            values.Add("api_user_name",userName);
53+
                byte[] respBytes = wc.UploadValues(ILoginURL, IQuery);
54-
            values.Add("api_user_password",pwd);
54+
55
56-
            using(WebClient wc = new WebClient()) {
56+
                if (resp.Contains("Bad API request"))
57-
                byte[] respBytes = wc.UploadValues(pastebin_login_url,values);
57+
                {
58
                    throw new WebException("Bad Request", WebExceptionStatus.SendFailure);
59-
                if(resp.Contains("Bad API request")) {
59+
60-
                    MessageBox.Show("Error:" + resp);
60+
61-
                    return null;
61+
                IUserKey = resp;
62
            }
63-
                _pastebinUserKey = resp;
63+
64
    }
65-
            return _pastebinUserKey;
65+
}