本帖最后由 hell01234 于 2015-3-20 23:21 编辑
[C#] 纯文本查看 复制代码 private string Stream2String(Stream st)
{
using (StreamReader sr = new StreamReader(st))
{
return sr.ReadToEnd();
}
}
private void button2_Click(object sender, EventArgs e)
{
//第一次请求
HttpClient hc1 = new HttpClient();
HttpWebResponse rp1= hc1.GetReponse("http://210.38.138.1:81/test.aspx", "get", null, false);
string html1 = Stream2String(rp1.GetResponseStream());
var cookie1 = rp1.Cookies;
MatchCollection matchs = Regex.Matches(html1, "<input type=\"hidden\" name=\"(__\\w+)\".* value=\"(.*)\" />");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < matchs.Count; i++)
{
sb.Append(matchs.Groups[1]).Append("=").Append(matchs.Groups[2]).Append("&");
}
string postData = sb.ToString() + "txtlogintype=0&txtUsername_Lib=201211621426&txtPas_Lib=qwaszx&btnLogin_Lib=登录";
postData = HttpUtility.UrlEncode(postData,Encoding.UTF8);
//第二次请求
CookieContainer cc1 = new CookieContainer();
cc1.Add(cookie1);
HttpClient hc2 = new HttpClient(cc1);
HttpWebResponse rp2= hc2.GetReponse("http://210.38.138.1:81/test.aspx","post",postData,false);
var cookie2 = rp2.Cookies;
//第三次请求
cc1.Add(cookie2);
HttpClient hc3= new HttpClient(cc1);
HttpWebResponse rp3 = hc3.GetReponse("http://210.38.138.1:81/user/userinfo.aspx", "get", null, true);
string htmlInfo = Stream2String(rp3.GetResponseStream());
}
|