我现在要模拟登录网站,从网上找许多代码都不好用 有谁做过详细介绍下啊~~~~
解决方案 »
- 问一下关于datalist或repeater分页的问题
- js中replace使用正则过滤
- 迷失了........?我刚24,现在面临抉择。是选一个人际关系好的地方但工资暂时不高,还是选择一个工资高的地方但人际关系不是很好!
- 请高手帮助---关于GridView出现的异常之解决方案
- 如何将"08 15 2006 1:36PM"转化为"2006-08-15 13:36:21"的形式??在线等..........
- 挑战高手,关于数据绑定的一个超难得问题!
- 我从数据库中读取图片时显示乱码,是怎么回事?请问怎么解决?
- 问题求教?
- 如何調用在ASP.NET的C# Script中,用<object></object>定義的控件?
- 微网站,如何调用手机自带播放器?
- <!--#include file="IncFile/T1.htm" -->后台怎样动态加载
- 多条件选择应该怎么实现?
Dictionary<string, string> postData,
ref CookieContainer objCookieContainer)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);
req.Method = "POST";
req.KeepAlive = true;
req.ContentType = "application/x-www-form-urlencoded";
//req.Timeout = 20; if (objCookieContainer == null)
objCookieContainer = new CookieContainer(); req.CookieContainer = objCookieContainer;
req.ContentLength = 0;
if (postData != null && postData.Count > 0)
{
StringBuilder objEncodedPostDatas = new StringBuilder();
foreach (var kv in postData)
{
objEncodedPostDatas.Append(HttpUtility.UrlEncode(kv.Key));
objEncodedPostDatas.Append("=");
objEncodedPostDatas.Append(HttpUtility.UrlEncode(kv.Value));
objEncodedPostDatas.Append("&");
}
byte[] byteData = Encoding.UTF8.GetBytes(objEncodedPostDatas.ToString().TrimEnd('&'));
req.ContentLength = byteData.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(byteData, 0, byteData.Length);
}
} var strResponse = "";
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
objCookieContainer = req.CookieContainer;
using (Stream resStream = res.GetResponseStream())
{
using (StreamReader sr = new StreamReader(resStream, Encoding.UTF8))
{
strResponse = sr.ReadToEnd();
}
}
}
return strResponse;
}调用:string uriStr = "http://xxxx/login.aspx";
string strId = "用户名";
string strPassword = "密码";var postData = new Dictionary<string, string>();
postData.Add("控件ID1", strId);
postData.Add("控件ID2", strPassword);
CookieContainer cc = new CookieContainer();
var response = RequestUrl(uriStr, postData, ref cc);
string uriStr = "http://my.chinahr.com/login.aspx?ReturnUrl=%2findex.aspx";
string strId = "[email protected]";
string strPassword = "123456abc"; Dictionary<string, string> postData = new Dictionary<string, string>();
postData.Add("LoginModule_ascx$tbUserName", strId);
postData.Add("LoginModule_ascx$tbPassword", strPassword);
CookieContainer cc = new CookieContainer();
string response = RequestUrl(uriStr, postData, ref cc);
Response.Write(response);上面是我调用写的代码,最后还是停留在登录页,帮忙看看啊
public static string RequestUrl(string strUrl, ref CookieContainer objCookieContainer)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);
req.Method = "GET";
req.CookieContainer = objCookieContainer;
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (Stream resStream = res.GetResponseStream())
{
objCookieContainer = req.CookieContainer;
using (StreamReader sr = new StreamReader(resStream, Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
}
}
string url = "http://my.chinahr.com/login.aspx?ReturnUrl=%2findex.aspx";
CookieContainer cc = new CookieContainer();
// 第一次GET请求,获取cookie
RequestUrl(url, ref cc);string strId = "[email protected]";
string strPassword = "123456abc";
Dictionary<string, string> postData = new Dictionary<string, string>();
postData.Add("LoginModule_ascx$tbUserName", strId);
postData.Add("LoginModule_ascx$tbPassword", strPassword);
postData.Add("LoginModule_ascx$btnLogin", "登录");string response = RequestUrl(url, postData, ref cc);
if (response.Contains("正在处理中,请您稍等..."))
Console.WriteLine("login success");string urlIndex = "http://my.chinahr.com/index.aspx";
response = RequestUrl(urlIndex, ref cc);