Advertisement
Guest User

Untitled

a guest
Jun 24th, 2011
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.IO;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         public static CookieContainer login(string url, string username, string password)
  21.         {
  22.             if (url.Length == 0 || username.Length == 0 || password.Length == 0)
  23.             {
  24.                 MessageBox.Show("Information Missing","Error");
  25.                 return null;
  26.             }
  27.  
  28.             CookieContainer myContainer = new CookieContainer();
  29.  
  30.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  31.             request.CookieContainer = new CookieContainer();
  32.  
  33.             // Set type to POST
  34.  
  35.             request.Method = "POST";
  36.             request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
  37.             request.ContentType = "application/x-www-form-urlencoded";
  38.             request.ProtocolVersion = HttpVersion.Version10;  // to get rid of the 417 error
  39.             ServicePointManager.Expect100Continue = true; // to get rid of the 417 error
  40.  
  41.             // Build the new header, this isn't a multipart/form, so it's very simple
  42.             StringBuilder data = new StringBuilder();
  43.             data.Append("username=" + Uri.EscapeDataString(username));
  44.             data.Append("&password=" + Uri.EscapeDataString(password));
  45.             data.Append("&login=Login");
  46.  
  47.             // Create a byte array of the data we want to send
  48.             byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
  49.  
  50.             // Set the content length in the request headers
  51.             request.ContentLength = byteData.Length;
  52.  
  53.             Stream postStream;
  54.             try
  55.             {
  56.                 postStream = request.GetRequestStream();
  57.             }
  58.             catch (Exception e)
  59.             {
  60.                 MessageBox.Show("Login - " + e.Message.ToString() + " (GRS)");
  61.                 return null;
  62.             }
  63.  
  64.             // Write data
  65.             postStream.Write(byteData, 0, byteData.Length);
  66.  
  67.             HttpWebResponse response;
  68.             try
  69.             {
  70.                 response = (HttpWebResponse)request.GetResponse();
  71.             }
  72.             catch (Exception e)
  73.             {
  74.                 MessageBox.Show("Login - " + e.Message.ToString() + " (GR)");
  75.                 return null;
  76.             }
  77.  
  78.             bool isLoggedIn = false;
  79.  
  80.             // Store the cookies
  81.             foreach (Cookie c in response.Cookies)
  82.             {
  83.                 if (c.Name.Contains("_u"))
  84.                 {
  85.                     if (Convert.ToInt32(c.Value) > 1)
  86.                     {
  87.                         isLoggedIn = true;
  88.                     }
  89.                 }
  90.                 myContainer.Add(c);
  91.             }
  92.  
  93.             if (isLoggedIn)
  94.             {
  95.                 return myContainer;
  96.             }
  97.             else
  98.             {
  99.                 return null;
  100.             }
  101.         }
  102.         private void button1_Click(object sender, EventArgs e)
  103.         {
  104.            
  105. CookieContainer boardCookies = login("http://localhost/ucp.php", this.textBox1.Text, this.textBox2.Text);
  106.  
  107.             if (boardCookies != null)
  108.                 MessageBox.Show("Your Login Was Successful, Welcome :}");
  109.             else
  110.                 MessageBox.Show("Login Failed, Recheck the Data Entered");
  111.         }
  112.  
  113.         // Submit to Poster.php from here
  114.        
  115.         private void button2_Click(object sender, EventArgs e)
  116.         {
  117.  
  118.  
  119.             // this is what we are sending
  120.             string post_data = "type=" + comboBox2.Text + "&subject=" + textBox4.Text + "&name=" + textBox3.Text + "&req=" + textBox5.Text + "&os=" + comboBox1.Text + "&overview=" + textBox7.Text + "&dlink=" + textBox8.Text;
  121.  
  122.             // this is where we will send it
  123.             string uri = "http://localhost/poster.php";
  124.  
  125.             // create a request
  126.             HttpWebRequest request = (HttpWebRequest)
  127.             WebRequest.Create(uri); request.KeepAlive = false;
  128.             request.ProtocolVersion = HttpVersion.Version10;
  129.             request.Method = "POST";
  130.  
  131.             // turn our request string into a byte stream
  132.             byte[] postBytes = Encoding.ASCII.GetBytes(post_data);
  133.  
  134.             // this is important - make sure you specify type this way
  135.             request.ContentType = "application/x-www-form-urlencoded";
  136.             request.ContentLength = postBytes.Length;
  137.             Stream requestStream = request.GetRequestStream();
  138.  
  139.             // now send it
  140.             requestStream.Write(postBytes, 0, postBytes.Length);
  141.             requestStream.Close();
  142.  
  143.  
  144.         }
  145.  
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement