问题描述
好的,我想创建一个c清晰的程序以给定凭据在外部网站上登录-到目前为止,该程序甚至可以在httpS网站上运行...但是存在一个问题-这是我的网站:).因此,我可以确定POST请求等等.事实是 因为我可以查看html源代码,所以可以在任何网站上确定POST请求.无论如何,问题在于,当我尝试登录外部网站时-根本不起作用.在下面,我为您提供Form的源代码+源代码 Form.Designer的页面+该程序的屏幕截图.
再次:该程序应在给定密码和用户名的任何网站上登录,这是怎么回事?我怀疑问题出在我如何处理Cookie上-但是我想我现在太懒了,无法更加注意Cookie的这一部分:).谢谢!!!
形式的源代码:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Text;
使用System.Windows.Forms;
使用System.Net;
使用System.IO;
使用System.Web;
命名空间登录
{
公共局部类Mainer:表格
{
公共Mainer()
{
InitializeComponent();
}
private void button1_Click(对象发送者,EventArgs e)
{
//& Submit2 =登录+登录
字符串n1 = textBox1.Text;
字符串n2 = textBox2.Text;
字符串postuser = textBox5.Text;
字符串postpassword = textBox6.Text;
// 字符串postData ="[email protected]& muti = somepass";
// "'+ postuser +'+'+ n1 +''&'+'+ postpassword +'
字符串adi = textBox7.Text;
字符串postData = postuser + n1 +&" +后密码+ n2 +&" + adi;
//字符串postData =" [email protected]& muti = somepass" ;;
String strresponse =";
// 字符串postData ="tuti='[email protected]'& muti = somepass'"; //准备由程序发送的postdata
byte [] byteArray = Encoding.UTF8.GetBytes(postData); //获取数据
// http://localhost/ji/default.php
字符串konro = textBox4.Text;
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(konro); //请求网站
request.Method ="POST"; //用于发送数据的方法
request.Accept ="text/html"; //方法
request.AllowAutoRedirect = true; //成功后重定向
//request.AuthenticationLevel =
request.UserAgent ="Mozilla/4.0(兼容; MSIE 6.0; Windows NT5.0; .NET CLR 1.0.2914)";
request.CookieContainer = new CookieContainer();
Cookie c =新的Cookie();
//string mana = Convert.ToString(c);
// MessageBox.Show(mana);
CookieContainer myContainer =新的CookieContainer();
// request.Accept ="image/*";
// request.Accept ="text/html"; //方法
request.AllowWriteStreamBuffering = true;
request.ProtocolVersion = HttpVersion.Version11;
// request.MaximumAutomaticRedirections = 4; //
// request.MaximumResponseHeadersLength = 4; //
request.ContentType ="application/x-www-form-urlencoded"; //
request.ContentLength = byteArray.Length; //
流newStream = request.GetRequestStream(); //打开连接
// oReq.CookieContainer =新的CookieContainer();
newStream.Write(byteArray,0,byteArray.Length); //发送数据.
request.Credentials = CredentialCache.DefaultNetworkCredentials;
// HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
WebHeaderCollection headerCollection =新的WebHeaderCollection();
HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
//获取与响应关联的流.
流receiveStream = response.GetResponseStream();
//将流以所需的编码格式传递到更高级别的流读取器.
StreamReader readStream =新的StreamReader(receiveStream,Encoding.UTF8);
strresponse = readStream.ReadToEnd();
// Response.Write(strresponse);
readStream.Close();
response.Close();
label1.Text =响应;
richTextBox1.Text = label1.Text;
// 结果程序Form2 =新的resulter();
// Form2.Show(); //= new Form2();
}
私有无效label1_Click(对象发送者,EventArgs e)
{
}
私有void Mainer_Load(对象发送者,EventArgs e)
{
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
设计师的源代码
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
命名空间登录
{
局部类Mainer
{
///< summary>
///必需的设计器变量.
///</summary>
私有System.ComponentModel.IContainer组件= null;
///< summary>
///清理所有正在使用的资源.
///</summary>
///< param name =" dispose>>如果应处置受管资源,则为true;否则为false.否则为false.</param>
受保护的重写void Dispose(布尔处置)
{
if(处置&&(组件!= null))
{
components.Dispose();
}
base.Dispose(处置);
}
#region Windows窗体设计器生成的代码
///< summary>
///支持Designer的必需方法-请勿修改
///使用代码编辑器将此方法的内容.
///</summary>
私有void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager资源=新的System.ComponentModel.ComponentResourceManager(typeof(Mainer));
this.button1 =新的System.Windows.Forms.Button();
this.label1 =新的System.Windows.Forms.Label();
this.richTextBox1 =新的System.Windows.Forms.RichTextBox();
this.textBox1 =新的System.Windows.Forms.TextBox();
this.textBox2 =新的System.Windows.Forms.TextBox();
this.textBox3 =新的System.Windows.Forms.TextBox();
this.textBox4 =新的System.Windows.Forms.TextBox();
this.label2 =新的System.Windows.Forms.Label();
this.label3 =新的System.Windows.Forms.Label();
this.label4 =新的System.Windows.Forms.Label();
this.label5 =新的System.Windows.Forms.Label();
this.textBox5 =新的System.Windows.Forms.TextBox();
this.textBox6 =新的System.Windows.Forms.TextBox();
this.label6 =新的System.Windows.Forms.Label();
this.label7 =新的System.Windows.Forms.Label();
this.textBox7 =新的System.Windows.Forms.TextBox();
this.label8 =新的System.Windows.Forms.Label();
this.SuspendLayout();
//
//button1
//
resources.ApplyResources(this.button1,"button1");
this.button1.Name ="button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click + =新的System.EventHandler(this.button1_Click);
//
//label1
//
resources.ApplyResources(this.label1,"label1");
this.label1.Name ="label1";
this.label1.Click + =新的System.EventHandler(this.label1_Click);
//
//richTextBox1
//
resources.ApplyResources(this.richTextBox1,"richTextBox1");
this.richTextBox1.MaximumSize = new System.Drawing.Size(1200,1200);
this.richTextBox1.Name ="richTextBox1";
//
//textBox1
//
resources.ApplyResources(this.textBox1,"textBox1");
this.textBox1.Name ="textBox1";
//
//textBox2
//
resources.ApplyResources(this.textBox2,"textBox2");
this.textBox2.Name ="textBox2";
//
//textBox3
//
resources.ApplyResources(this.textBox3,"textBox3");
this.textBox3.Name ="textBox3";
//
//textBox4
//
this.textBox4.AcceptsReturn = true;
this.textBox4.AcceptsTab = true;
this.textBox4.AutoCompleteCustomSource.AddRange(new string [] {
resources.GetString(" textBox4.AutoCompleteCustomSource")}));
resources.ApplyResources(this.textBox4,"textBox4");
this.textBox4.Name ="textBox4";
//
//label2
//
resources.ApplyResources(this.label2,"label2");
this.label2.BackColor = System.Drawing.Color.Transparent;
this.label2.Name ="label2";
//
//label3
//
resources.ApplyResources(this.label3,"label3");
this.label3.BackColor = System.Drawing.Color.Transparent;
this.label3.Name ="label3";
//
//label4
//
resources.ApplyResources(this.label4,"label4");
this.label4.BackColor = System.Drawing.Color.Transparent;
this.label4.Name ="label4";
//
//label5
//
resources.ApplyResources(this.label5,"label5");
this.label5.BackColor = System.Drawing.Color.Transparent;
this.label5.Name ="label5";
//
//textBox5
//
resources.ApplyResources(this.textBox5,"textBox5");
this.textBox5.Name ="textBox5";
//
//textBox6
//
resources.ApplyResources(this.textBox6,"textBox6");
this.textBox6.Name ="textBox6";
//
//label6
//
resources.ApplyResources(this.label6,"label6");
this.label6.BackColor = System.Drawing.Color.Transparent;
this.label6.Name ="label6";
//
//label7
//
resources.ApplyResources(this.label7,"label7");
this.label7.BackColor = System.Drawing.Color.Transparent;
this.label7.Name ="label7";
//
//textBox7
//
resources.ApplyResources(this.textBox7,"textBox7");
this.textBox7.Name ="textBox7";
//
//label8
//
resources.ApplyResources(this.label8,"label8");
this.label8.BackColor = System.Drawing.Color.Transparent;
this.label8.Name ="label8";
//
//Mainer
//
resources.ApplyResources(this,"$ this");
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.label8);
this.Controls.Add(this.textBox7);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.IsMdiContainer = true;
this.Name ="Mainer";
this.Opacity = 0.9;
this.ShowIcon = false;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load + =新的System.EventHandler(this.Mainer_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
私人System.Windows.Forms.Button button1;
公共System.Windows.Forms.Label label1;
私人System.Windows.Forms.RichTextBox richTextBox1;
私人System.Windows.Forms.TextBox textBox1;
私人System.Windows.Forms.TextBox textBox2;
私人System.Windows.Forms.TextBox textBox3;
私人System.Windows.Forms.TextBox textBox4;
私有System.Windows.Forms.Label label2;
私有System.Windows.Forms.Label label3;
私有System.Windows.Forms.Label label4;
私有System.Windows.Forms.Label label5;
私人System.Windows.Forms.TextBox textBox5;
私人System.Windows.Forms.TextBox textBox6;
私有System.Windows.Forms.Label label6;
私有System.Windows.Forms.Label label7;
私人System.Windows.Forms.TextBox textBox7;
私有System.Windows.Forms.Label label8;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
屏幕截图
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
< img src =" http://i47.tinypic.com/2vuavd4.jpg">
OK, I want to create a c sharp program to login on external website given credentials - the program so far works even on a httpS website...but there's one problem - it's my website :). Hence I can determine the POST request and so on. The thing is that determining POST request can be done on any website since I can look at the html source code. Anyway, the problem is that when I try to login on external websites - it just doesn't work. Below I am giving you the source code of the Form + the source code of the Form.Designer + screenshot of the program.
Once again: the program should login on any website given password and username, what's wrong? I suspect the problem lies in how I am handling cookies - but I guess I am too lazy now to pay closer attention to that cookie part :). Thanks!!!
Souce code of the Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
namespace logins
{
public partial class Mainer : Form
{
public Mainer()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// &Submit2=Log+In
string n1 = textBox1.Text;
string n2 = textBox2.Text;
string postuser = textBox5.Text;
string postpassword = textBox6.Text;
// string postData = "[email protected]&muti=somepass";
// "'+postuser+' + '+n1+''&'+'+postpassword+'
string adi = textBox7.Text;
string postData = postuser + n1 + "&" + postpassword + n2 + "&" + adi;
// string postData = "[email protected]&muti=somepass";
String strresponse="";
// String postData = "tuti='[email protected]'&muti=somepass'"; //prepare postdata send by the program
byte[] byteArray = Encoding.UTF8.GetBytes(postData); //get the data
// http://localhost/ji/default.php
string konro = textBox4.Text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(konro); //request the website
request.Method = "POST"; //method used to send data
request.Accept = "text/html"; //method
request.AllowAutoRedirect = true; //redirect after success
// request.AuthenticationLevel=
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT5.0; .NET CLR 1.0.2914)";
request.CookieContainer = new CookieContainer();
Cookie c = new Cookie();
//string mana = Convert.ToString(c);
// MessageBox.Show(mana);
CookieContainer myContainer = new CookieContainer();
// request.Accept="image/*";
// request.Accept = "text/html"; //method
request.AllowWriteStreamBuffering = true;
request.ProtocolVersion = HttpVersion.Version11;
// request.MaximumAutomaticRedirections = 4; //
// request.MaximumResponseHeadersLength = 4; //
request.ContentType = "application/x-www-form-urlencoded"; //
request.ContentLength = byteArray.Length; //
Stream newStream = request.GetRequestStream(); //open connction
// oReq.CookieContainer = new CookieContainer();
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
request.Credentials = CredentialCache.DefaultNetworkCredentials;
// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
WebHeaderCollection headerCollection = new WebHeaderCollection();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
strresponse = readStream.ReadToEnd();
// Response.Write(strresponse);
readStream.Close();
response.Close();
label1.Text = strresponse;
richTextBox1.Text = label1.Text;
// resulter Form2 = new resulter();
// Form2.Show(); // = new Form2();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Mainer_Load(object sender, EventArgs e)
{
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SOURCE CODE OF the DESIGNER
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace logins
{
partial class Mainer
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Mainer));
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.textBox7 = new System.Windows.Forms.TextBox();
this.label8 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
resources.ApplyResources(this.button1, "button1");
this.button1.Name = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
resources.ApplyResources(this.label1, "label1");
this.label1.Name = "label1";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// richTextBox1
//
resources.ApplyResources(this.richTextBox1, "richTextBox1");
this.richTextBox1.MaximumSize = new System.Drawing.Size(1200, 1200);
this.richTextBox1.Name = "richTextBox1";
//
// textBox1
//
resources.ApplyResources(this.textBox1, "textBox1");
this.textBox1.Name = "textBox1";
//
// textBox2
//
resources.ApplyResources(this.textBox2, "textBox2");
this.textBox2.Name = "textBox2";
//
// textBox3
//
resources.ApplyResources(this.textBox3, "textBox3");
this.textBox3.Name = "textBox3";
//
// textBox4
//
this.textBox4.AcceptsReturn = true;
this.textBox4.AcceptsTab = true;
this.textBox4.AutoCompleteCustomSource.AddRange(new string[] {
resources.GetString("textBox4.AutoCompleteCustomSource")});
resources.ApplyResources(this.textBox4, "textBox4");
this.textBox4.Name = "textBox4";
//
// label2
//
resources.ApplyResources(this.label2, "label2");
this.label2.BackColor = System.Drawing.Color.Transparent;
this.label2.Name = "label2";
//
// label3
//
resources.ApplyResources(this.label3, "label3");
this.label3.BackColor = System.Drawing.Color.Transparent;
this.label3.Name = "label3";
//
// label4
//
resources.ApplyResources(this.label4, "label4");
this.label4.BackColor = System.Drawing.Color.Transparent;
this.label4.Name = "label4";
//
// label5
//
resources.ApplyResources(this.label5, "label5");
this.label5.BackColor = System.Drawing.Color.Transparent;
this.label5.Name = "label5";
//
// textBox5
//
resources.ApplyResources(this.textBox5, "textBox5");
this.textBox5.Name = "textBox5";
//
// textBox6
//
resources.ApplyResources(this.textBox6, "textBox6");
this.textBox6.Name = "textBox6";
//
// label6
//
resources.ApplyResources(this.label6, "label6");
this.label6.BackColor = System.Drawing.Color.Transparent;
this.label6.Name = "label6";
//
// label7
//
resources.ApplyResources(this.label7, "label7");
this.label7.BackColor = System.Drawing.Color.Transparent;
this.label7.Name = "label7";
//
// textBox7
//
resources.ApplyResources(this.textBox7, "textBox7");
this.textBox7.Name = "textBox7";
//
// label8
//
resources.ApplyResources(this.label8, "label8");
this.label8.BackColor = System.Drawing.Color.Transparent;
this.label8.Name = "label8";
//
// Mainer
//
resources.ApplyResources(this, "$this");
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.label8);
this.Controls.Add(this.textBox7);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.IsMdiContainer = true;
this.Name = "Mainer";
this.Opacity = 0.9;
this.ShowIcon = false;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Mainer_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
public System.Windows.Forms.Label label1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox textBox7;
private System.Windows.Forms.Label label8;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SCREENSHOT
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<img src="http://i47.tinypic.com/2vuavd4.jpg">
推荐答案
如果您懒于寻找,我们为什么要寻找您.我猜.
If you're lazy to look, why should we look for u. I guess.
这篇关于通过C Sharp在网站上自动登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!