C#如何获得需要帐户密码的网站的某些网页,帐户密码已知道各位高手,请教了。这个问题搞得我头晕脑胀,请大家帮忙。
我需要把一个网站上的很多页面抓下来,这个网站是需要帐户、密码的,但我已经知道帐户和密码了。如 username="user100",password="333123"
网站页面如,http://www.mysteel.com/servlet/News.Index?type=53
我需要把这个页面上的链接对应的页面内容全部抓下来,我跟怎么办呢?
链接标题如:2005年1月份全国重点大中型钢铁企业钢铁产量统计
链接地址如:http://www.mysteel.com/servlet/News.Detail?id=824541
http://www.mysteel.com/servlet/News.Detail?id=824540
http://www.mysteel.com/servlet/News.Detail?id=824539
这些页面需要输入帐号密码,登陆之后才可以看到。如果没有登陆,则显示要求注册或登陆的页面。
可惜,我的程序发出的请求就是得不到真实的页面,而只能得到要求注册登陆的页面。请各位大侠多帮帮忙!
我的程序:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;using System.Text;
using System.IO;
namespace Banyiweb
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBoxWeb;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button3;
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent(); } protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBoxWeb = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(0, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 0;
this.label1.Text = "请输入网址:";
this.textBoxWeb.Location = new System.Drawing.Point(80, 8);
this.textBoxWeb.Name = "textBoxWeb";
this.textBoxWeb.Size = new System.Drawing.Size(344, 21);
this.textBoxWeb.TabIndex = 1;
this.textBoxWeb.Text = "http://www.mysteel.com/servlet/News.Detail?id=824541";
this.button1.Location = new System.Drawing.Point(208, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 2;
this.button1.Text = "提交";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.statusBar1.Location = new System.Drawing.Point(0, 423);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(640, 22);
this.statusBar1.TabIndex = 5;
this.statusBar1.Text = "http://";
this.richTextBox1.Location = new System.Drawing.Point(0, 256);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(632, 160);
this.richTextBox1.TabIndex = 6;
this.richTextBox1.Text = "请正确输入网址,在程序找不到网址的时候会变慢";
this.button2.Location = new System.Drawing.Point(552, 40);
this.button2.Name = "button2";
this.button2.TabIndex = 7;
this.button2.Text = "分析网页";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.richTextBox2.Location = new System.Drawing.Point(8, 64);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(624, 184);
this.richTextBox2.TabIndex = 8;
this.richTextBox2.Text = "";
this.textBox1.Location = new System.Drawing.Point(456, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(88, 21);
this.textBox1.TabIndex = 9;
this.textBox1.Text = "/";
this.button3.Location = new System.Drawing.Point(552, 16);
this.button3.Name = "button3";
this.button3.TabIndex = 10;
this.button3.Text = "button3";
this.button3.Click += new System.EventHandler(this.button3_Click);
this.AutoScale = false;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(640, 445);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBoxWeb);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "网页源代码";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
string webstr;
webstr=textBoxWeb.Text;
if (webstr.IndexOf("http://") <0)
{
MessageBox.Show("出错了 BANYI","你输入的网址有误");
textBoxWeb.Text="http://www.";
return;
}
button1.Enabled=false;
HttpWebResponse res;
HttpWebRequest req;
try
{
req=(HttpWebRequest)WebRequest.Create(webstr);
//req=(HttpWebRequest)WebRequest.Create(new NetworkCredential("username","password"), webstr);//正确密码没有给出
req.GetResponse().GetResponseStream();
res=(HttpWebResponse)req.GetResponse();
}
catch (Exception)
{
MessageBox.Show("对不起找不到该网址,网址格式如:http://www.163.com","出错了 伙计!");
button1.Enabled=true;
return;
}
textBoxWeb.Text="正在读取中....";
StreamReader strm=new StreamReader(res.GetResponseStream(),Encoding.GetEncoding(54936));//简体中文 Encoding.GetEncoding(54936),Encoding.ASCII
String sLine;
richTextBox1.Text="";
do //学习笔记do循环C# do while 循环体 loop do until 循环体 loop
{
sLine=strm.ReadLine();
richTextBox1.Text=richTextBox1.Text+sLine;
}
while (sLine!=null);
strm.Close();
textBoxWeb.Text="http://www.";
button1.Enabled=true;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
} private void Form1_Load(object sender, System.EventArgs e)
{
} private void button3_Click(object sender, System.EventArgs e)
{
//test
string strURL1,strTitle1;
strURL1=@"http://www.mysteel.com/servlet/News.Detail?id=824541";
strTitle1="2005年1月份全国重点大中型钢铁企业钢铁产量统计";
//2005年1月份全国重点大中型钢铁企业钢铁产量统计 http://www.mysteel.com/servlet/News.Detail?id=824541
getwebpage(strURL1,strTitle1); } }
}
我需要把一个网站上的很多页面抓下来,这个网站是需要帐户、密码的,但我已经知道帐户和密码了。如 username="user100",password="333123"
网站页面如,http://www.mysteel.com/servlet/News.Index?type=53
我需要把这个页面上的链接对应的页面内容全部抓下来,我跟怎么办呢?
链接标题如:2005年1月份全国重点大中型钢铁企业钢铁产量统计
链接地址如:http://www.mysteel.com/servlet/News.Detail?id=824541
http://www.mysteel.com/servlet/News.Detail?id=824540
http://www.mysteel.com/servlet/News.Detail?id=824539
这些页面需要输入帐号密码,登陆之后才可以看到。如果没有登陆,则显示要求注册或登陆的页面。
可惜,我的程序发出的请求就是得不到真实的页面,而只能得到要求注册登陆的页面。请各位大侠多帮帮忙!
我的程序:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;using System.Text;
using System.IO;
namespace Banyiweb
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBoxWeb;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.RichTextBox richTextBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button3;
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent(); } protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBoxWeb = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(0, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 0;
this.label1.Text = "请输入网址:";
this.textBoxWeb.Location = new System.Drawing.Point(80, 8);
this.textBoxWeb.Name = "textBoxWeb";
this.textBoxWeb.Size = new System.Drawing.Size(344, 21);
this.textBoxWeb.TabIndex = 1;
this.textBoxWeb.Text = "http://www.mysteel.com/servlet/News.Detail?id=824541";
this.button1.Location = new System.Drawing.Point(208, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 2;
this.button1.Text = "提交";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.statusBar1.Location = new System.Drawing.Point(0, 423);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(640, 22);
this.statusBar1.TabIndex = 5;
this.statusBar1.Text = "http://";
this.richTextBox1.Location = new System.Drawing.Point(0, 256);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(632, 160);
this.richTextBox1.TabIndex = 6;
this.richTextBox1.Text = "请正确输入网址,在程序找不到网址的时候会变慢";
this.button2.Location = new System.Drawing.Point(552, 40);
this.button2.Name = "button2";
this.button2.TabIndex = 7;
this.button2.Text = "分析网页";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.richTextBox2.Location = new System.Drawing.Point(8, 64);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(624, 184);
this.richTextBox2.TabIndex = 8;
this.richTextBox2.Text = "";
this.textBox1.Location = new System.Drawing.Point(456, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(88, 21);
this.textBox1.TabIndex = 9;
this.textBox1.Text = "/";
this.button3.Location = new System.Drawing.Point(552, 16);
this.button3.Name = "button3";
this.button3.TabIndex = 10;
this.button3.Text = "button3";
this.button3.Click += new System.EventHandler(this.button3_Click);
this.AutoScale = false;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(640, 445);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBoxWeb);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "网页源代码";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
string webstr;
webstr=textBoxWeb.Text;
if (webstr.IndexOf("http://") <0)
{
MessageBox.Show("出错了 BANYI","你输入的网址有误");
textBoxWeb.Text="http://www.";
return;
}
button1.Enabled=false;
HttpWebResponse res;
HttpWebRequest req;
try
{
req=(HttpWebRequest)WebRequest.Create(webstr);
//req=(HttpWebRequest)WebRequest.Create(new NetworkCredential("username","password"), webstr);//正确密码没有给出
req.GetResponse().GetResponseStream();
res=(HttpWebResponse)req.GetResponse();
}
catch (Exception)
{
MessageBox.Show("对不起找不到该网址,网址格式如:http://www.163.com","出错了 伙计!");
button1.Enabled=true;
return;
}
textBoxWeb.Text="正在读取中....";
StreamReader strm=new StreamReader(res.GetResponseStream(),Encoding.GetEncoding(54936));//简体中文 Encoding.GetEncoding(54936),Encoding.ASCII
String sLine;
richTextBox1.Text="";
do //学习笔记do循环C# do while 循环体 loop do until 循环体 loop
{
sLine=strm.ReadLine();
richTextBox1.Text=richTextBox1.Text+sLine;
}
while (sLine!=null);
strm.Close();
textBoxWeb.Text="http://www.";
button1.Enabled=true;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
} private void Form1_Load(object sender, System.EventArgs e)
{
} private void button3_Click(object sender, System.EventArgs e)
{
//test
string strURL1,strTitle1;
strURL1=@"http://www.mysteel.com/servlet/News.Detail?id=824541";
strTitle1="2005年1月份全国重点大中型钢铁企业钢铁产量统计";
//2005年1月份全国重点大中型钢铁企业钢铁产量统计 http://www.mysteel.com/servlet/News.Detail?id=824541
getwebpage(strURL1,strTitle1); } }
}
解决方案 »
- 如何给标题栏命名当前登陆用户名?
- C# 如何利用时间控制Console.WriteLine
- 怎样实现cs与bs之间的通信?up有分
- C#、C++和Java中重载和覆盖的一点问题
- [超高分]怎样把从数据库里取得的数据生成相应的层次结构图——如果能解决我将源码献出供大家分享
- 如何找出xml中符全条件的记录,并显示在datagrid里面?
- emgucv的配置
- C# 控件名称为何无法修改?
- ADO.net连接access2007数据库插入语句,删除语句,查找语句,苦逼学生党求大神帮忙
- 我想提个关于datagrid的事件问题!
- 请问C#调用存储过程用DATAGRID控件不能分页要如何修改?
- 如何用C#实现键盘测试---高手请进
Dim strFormData As String strFormData = "UserName=****&Password=******&sURL=%2Fservlet%2FNews.Detail%3Fid%3D843442&x=38&y=8"
Inet1.Execute strURL, "Post", strFormData, "Content-Type: application/x-www-form-urlencoded "
Do Until Inet1.StillExecuting = False '这里阻塞前面的Inet1,确保登陆成功之后再取页面,你可以将此Do取消了试一次。
DoEvents
Loop
Inet1.Cancel WebBrowser1.Navigate strURL
End Sub
最后两个参数是用户名,密码