Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public static CookieContainer login(string url, string username, string password)
- {
- if (url.Length == 0 || username.Length == 0 || password.Length == 0)
- {
- MessageBox.Show("Information Missing","Error");
- return null;
- }
- CookieContainer myContainer = new CookieContainer();
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.CookieContainer = new CookieContainer();
- // Set type to POST
- request.Method = "POST";
- request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
- request.ContentType = "application/x-www-form-urlencoded";
- request.ProtocolVersion = HttpVersion.Version10; // to get rid of the 417 error
- ServicePointManager.Expect100Continue = true; // to get rid of the 417 error
- // Build the new header, this isn't a multipart/form, so it's very simple
- StringBuilder data = new StringBuilder();
- data.Append("username=" + Uri.EscapeDataString(username));
- data.Append("&password=" + Uri.EscapeDataString(password));
- data.Append("&login=Login");
- // Create a byte array of the data we want to send
- byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
- // Set the content length in the request headers
- request.ContentLength = byteData.Length;
- Stream postStream;
- try
- {
- postStream = request.GetRequestStream();
- }
- catch (Exception e)
- {
- MessageBox.Show("Login - " + e.Message.ToString() + " (GRS)");
- return null;
- }
- // Write data
- postStream.Write(byteData, 0, byteData.Length);
- HttpWebResponse response;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (Exception e)
- {
- MessageBox.Show("Login - " + e.Message.ToString() + " (GR)");
- return null;
- }
- bool isLoggedIn = false;
- // Store the cookies
- foreach (Cookie c in response.Cookies)
- {
- if (c.Name.Contains("_u"))
- {
- if (Convert.ToInt32(c.Value) > 1)
- {
- isLoggedIn = true;
- }
- }
- myContainer.Add(c);
- }
- if (isLoggedIn)
- {
- return myContainer;
- }
- else
- {
- return null;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- CookieContainer boardCookies = login("http://localhost/ucp.php", this.textBox1.Text, this.textBox2.Text);
- if (boardCookies != null)
- MessageBox.Show("Your Login Was Successful, Welcome :}");
- else
- MessageBox.Show("Login Failed, Recheck the Data Entered");
- }
- // Submit to Poster.php from here
- private void button2_Click(object sender, EventArgs e)
- {
- // this is what we are sending
- string post_data = "type=" + comboBox2.Text + "&subject=" + textBox4.Text + "&name=" + textBox3.Text + "&req=" + textBox5.Text + "&os=" + comboBox1.Text + "&overview=" + textBox7.Text + "&dlink=" + textBox8.Text;
- // this is where we will send it
- string uri = "http://localhost/poster.php";
- // create a request
- HttpWebRequest request = (HttpWebRequest)
- WebRequest.Create(uri); request.KeepAlive = false;
- request.ProtocolVersion = HttpVersion.Version10;
- request.Method = "POST";
- // turn our request string into a byte stream
- byte[] postBytes = Encoding.ASCII.GetBytes(post_data);
- // this is important - make sure you specify type this way
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = postBytes.Length;
- Stream requestStream = request.GetRequestStream();
- // now send it
- requestStream.Write(postBytes, 0, postBytes.Length);
- requestStream.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement