C#模拟登录总结
/**//// <summary> 登录
/// </summary>
/// <param name="url"></param>
/// <param name="paramList"></param>
/// <returns></returns>
public static string Login(String url, String paramList)
{
HttpWebRespon res = null;
string strResult = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = fal;
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
StringBuilder UrlEncoded = new StringBuilder();
Char[] rerved = { '?', '=', '&' };
byte[] SomeBytes = null;
if (paramList != null)
{
路遥马亡
int i = 0, j;
while (i < paramList.Length)
{
j = paramList.IndexOfAny(rerved, i);
if (j == -1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
玫瑰花的简介UrlEncoded.Append(paramList.Substring(j, 1));
i = j + 1;
关于国庆节的手抄报
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Clo();
}
el
{
req.ContentLength = 0;
}
res = (HttpWebRespon)req.GetRespon();
cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
Stream ReceiveStream = res.GetResponStream();
Encoding encode = System.Text.Encoding.GetEncoding("GBK");
StreamReader sr = new StreamReader(ReceiveStream, encode);
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
}
catch (Exception e)
{
strResult = e.ToString();
}
finally
{
if (res != null)
{
res.Clo();普洛斯是哪个国家的
}
}
return strResult;
}
/**//// <summary> 获取页面HTML
/// </summary>
/// <param name="url"></param>
直到相思了无益/// <param name="paramList"></param>
/
// <returns></returns>
public static string getPage(String url, String paramList)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Headers["If-None-Match"] = "36d0ed736e88c71:d9f";
req.Referer = "website/login.do";
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
req.CookieContainer.SetCookies(new Uri(url), cookieheader);
HttpWebRespon res = (HttpWebRespon)req.GetRespon();
StreamReader sr = new StreamReader(res.GetResponStream(),Encoding.Default);
string strResult = sr.ReadToEnd();
sr.Clo();
return strResult;
}调用:
string postData = "urName=admin&password=pass&area=2006&Submit=%B5%C7+%C2%BC";
string strLogin, strResult;
strLogin = Login("website/login.do", postData);
strResult = getPage("website/tohjtree.do", "");
//输出
this.webBrowr1.Document.Write(strResult);
C#(WINFORM)实现模拟POST发送请求登录网站
作者:不详来源:整理发布时间:2007-8-5 14:29:24 发布人:Polaris
龙头简笔画减小字体增大字体
最近接了个小项目,用到一个技术需要模拟POST向Web服务器发送请求来进行登录,下面写一下主要代码。
string strId = "admin";//用户名
string strPassword = "xxxxx";//密码
左手五个//string strsubmit = "YES";
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "urname=" + strId;
postData += ("&password=" + strPassword);
//postData += ("&Accept=" + strsubmit);
byte[] data = encoding.GetBytes(postData);
// Prepare
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("/login.aspx");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Clo();
// Get respon
HttpWebRespon myRespon = (HttpWebRespon)myRequest.GetRespon();
StreamReader reader = new StreamReader(myRespon.GetResponStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
//Respon.Write(content);
textBox1.Text = content;
通过HttpWebRequest 发送POST 请求实现自动登陆[ 2006-09-15 16:17:12 | 作者: 景裔] 字体大小: 大| 中| 小
怎样通过HttpWebRequest 发送POST 请求到一个网页服务器?例如编写个程序实现自动用户登录,自动提交表单数据到网站等。
假如某个页面有个如下的表单(Form):view plaincopy to clipboardprint?
<form name="form1" action="http:/login.asp" method="post">
<input type="text" name="urid" value="">
<input type="password" name="password" value="">
</form>
<form name="form1" action="http:/login.asp" method="post">
<input type="text" name="urid" value="">
<input type="password" name="password" value="">
</form>从表单可看到表单有两个表单域,一个是urid另一个是password,所以以POST 形式提交的数据应该包含有这两项。
其中POST的数据格式为:
表单域名称1=值1&表单域名称2=值2&表单域名称3=值3……
要注意的是“值”必须是经过HTMLEncode的,即不能包含“<>=&”这些符号。
本例子要提交的数据应该是:
urid=value1&password=value2
用C#写提交程序:view plaincopy to clipboardprint?
string strId = "guest";
string strPassword= "123456";
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="urid="+strId;
postData += ("&password="+strPassword);
byte[] data = encoding.GetBytes(postData);
手术室设备// Prepare
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http:/login.asp");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Clo();
// Get respon
HttpWebRespon myRespon=(HttpWebRespon)myRequest.GetRespon(); StreamReader reader = new StreamReader(respon.GetResponStream(),Encoding.Default); string content = reader.ReadToEnd();
Console.WriteLine(content);
string strId = "guest";
string strPassword= "123456";
ASCIIEncoding encoding=new ASCIIEncoding();
string postData="urid="+strId;
postData += ("&password="+strPassword);
byte[] data = encoding.GetBytes(postData);
// Prepare
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http:/login.asp");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
/
/ Send the data.
newStream.Write(data,0,data.Length);