HttpWebRequest实现模拟登陆 并抓取登陆后的页面信息

zq826348886 2010-04-03 05:21:38
急急急!!!……
现在我有两个系统,一个A系统,是外网的,一个B系统,我现在要在B系统中用HttpWebRequest实现模拟登陆
A系统,并抓取A系统中的数据。假如A系统中有个登陆页面LoginA.aspx,还有个ListA.aspx页面,A系统登陆
后会设置一个Session,在进入ListA.aspx页面时也要验证Session,现在要实现的功能是我要在我的B系统中
模拟登陆进A系统,并且获取A系统中ListA.aspx页面里的内容。
跪求具体代码。。。
...全文
818 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdfasdf2w2 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 cheungqin 的回复:]
好说好说 邮箱发我 我有示例
[/Quote]
给一份啊大侠。。。 444261689@qq.com
youbingbingke820 2011-11-21
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 cheungqin 的回复:]
好说好说 邮箱发我 我有示例
[/Quote]

给一份啊大侠。。。 youbing820@126.com
星先 2011-08-27
  • 打赏
  • 举报
回复
...也给我一份..
scys1217@sohu.com
qq397465 2011-07-21
  • 打赏
  • 举报
回复
能不能给我一份
zq826348886 2010-04-06
  • 打赏
  • 举报
回复
zq826348886@qq.com 谢啦 兄弟~~~~
CheungQin 2010-04-06
  • 打赏
  • 举报
回复
好说好说 邮箱发我 我有示例
lpyqyc 2010-04-04
  • 打赏
  • 举报
回复
这是一个类似单点登录的问题,你在网上搜单点登录的解决方案,就能解决你的问题。
ycwj88 2010-04-04
  • 打赏
  • 举报
回复
之前搞单点登录的时候玩过,httpwebrequest模拟一个post请求。
newdigitime 2010-04-04
  • 打赏
  • 举报
回复
先人工登录.
然后用抓包工具得到相关的cookie键值. 加入到httpwebrequest的发送数据包里.
zq826348886 2010-04-04
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 newdigitime 的回复:]
先人工登录.
然后用抓包工具得到相关的cookie键值. 加入到httpwebrequest的发送数据包里.
[/Quote]

这个方法确实可行 但我要的不是这种效果啊 我想做的是在B系统中输入用户名密码来登录A系统。。。
zhouzangood 2010-04-03
  • 打赏
  • 举报
回复
upupup
wuyq11 2010-04-03
  • 打赏
  • 举报
回复
在PostAndGetHTML里先登录,再httpwebrequest抓取数据
登录后out CookieContainer值
bingo_ 2010-04-03
  • 打赏
  • 举报
回复
只是一个保持Cookie的问题
http://www.cnblogs.com/yidanda/archive/2009/07/22/1528530.html
zq826348886 2010-04-03
  • 打赏
  • 举报
回复
大侠都进来切磋切磋啦~~~~ 跪等3天
hxn1987420 2010-04-03
  • 打赏
  • 举报
回复
学习中,路过顶贴!!!!
zq826348886 2010-04-03
  • 打赏
  • 举报
回复
谁解决了所有分值全部奉上,再加101分总共200分,谢谢各位大侠了,急急急
zq826348886 2010-04-03
  • 打赏
  • 举报
回复

以下为我写的测试代码 仅供参考,在登陆后,如果成功,返回的Html流应该有【登陆成功】,但是现在没有返回。还有当我再请求第二个页面ListA.aspx时,会报:远程服务器返回错误: (500) 内部服务器错误。
各位大侠帮帮忙啊



//-------------------------------B系统代码,点击Button1模拟登陆------------------

protected void Button1_Click(object sender, EventArgs e)
{
try
{
CookieContainer cc = new CookieContainer();//this is for keep the Session and Cookie

Hashtable param = new Hashtable();//this is for keep post data.

//以下地址是A系统的登录地址
string urlLogin = "http://192.168.1.20:10031/LoginA.aspx";
param.Add("txtUs", "wang");
param.Add("txtPa", "1234");
param.Add("Button1", "Button");
string result = PostAndGetHTML(urlLogin, cc, param);
param.Clear();
//A系统里的ListA.aspx页面
string url2 = "http://192.168.1.20:10031/ListA.aspx";
param = new Hashtable();
result = PostAndGetHTML(url2, cc, param);
}
catch (WebException we)
{
string msg = we.Message;
}

}



public string PostAndGetHTML(string targetURL, CookieContainer cc, Hashtable param)
{
try
{
//prepare the submit data
string formData = "";

foreach (DictionaryEntry de in param)
{
formData += de.Key.ToString() + "=" + de.Value.ToString() + "&";
}

if (formData.Length > 0)
formData = formData.Substring(0, formData.Length - 1); //remove last '&'

ASCIIEncoding encoding = new ASCIIEncoding();

byte[] data = encoding.GetBytes(formData);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(targetURL));

request.CookieContainer = cc;
request.Method = "POST"; //post
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.Referer = targetURL;
request.KeepAlive = true;

Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = cc.GetCookies(request.RequestUri);
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();

return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

}



//-------------------------------A系统代码------------------


LoginA.aspx
-----------HTML代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Test003.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" action="" method="post">
<div>
<table>
<tr>
<td>用户名:</td>
<td>
<input type="text" id="txtUs" name="txtUs" /></td>

<tr>
<td>密码:</td>
<td><input type="text" id="txtPa" name="txtPa" /></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>



----------C#代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Test003
{
public partial class LoginA : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
string name = Request.Form["txtUs"];
string pass = Request.Form["txtPa"];
//string [] name = Request.Form.GetValues("txtUs");
//string[] pass = Request.Form.GetValues("txtPa");
if (name == "wang" && pass == "1234")
{
this.Label1.Text = "登陆成功!";
Session["wang"] = "wang";
}
else
{
this.Label1.Text = "登陆失败";
Session["wang"] = "shao";
}
}
}
}


ListA.aspx页面

-------------------HTML代码,只有一个Label,用户显示

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Test003.WebForm3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>


----------------C#代码,给Label赋值

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Test003
{
public partial class ListA : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ab = Session["wang"].ToString();
this.Label1.Text = ab;

}
}
}




wanshaoxia 2010-04-03
  • 打赏
  • 举报
回复
怎么没人啊,顶起来
chenpk0912 2010-04-03
  • 打赏
  • 举报
回复
模拟post请求
具体得到session 搞不清楚
期待高手
wanshaoxia 2010-04-03
  • 打赏
  • 举报
回复
靠,我也想知道,顶,高手进来啊

62,052

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧