winform模拟登陆网页_C#模拟登录

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.Web;usingSystem.IO;usingSystem.Collections;namespace模拟登录

{classProgram

{publicstaticCookieContainer cc=newCookieContainer();staticvoidMain(string[] args)

{

CookieContainer cc=newCookieContainer();//this is for keep the Session and CookieHashtable param=newHashtable();//this is for keep post data.//这个地址是我自己IIS上的地址stringurlLogin="http://www.my.com/CS.aspx";

param.Add("txtName","fxl");

param.Add("txtPwd","123");stringresult=PostAndGetHTML(urlLogin,refcc, param);

Console.WriteLine(result);

}publicstaticstringPostAndGetHTML(stringtargetURL,refCookieContainer cc, Hashtable param)

{stringformData="";foreach(DictionaryEntry deinparam)

{

formData+=de.Key.ToString()+"="+de.Value.ToString()+"&";

}if(formData.Length>0)

formData=formData.Substring(0, formData.Length-1);//remove last '&'ASCIIEncoding encoding=newASCIIEncoding();byte[] data=encoding.GetBytes(formData);

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

request.Method="POST";//postrequest.ContentType="application/x-www-form-urlencoded";

request.ContentLength=data.Length;

request.UserAgent="Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)";

Stream newStream=request.GetRequestStream();

newStream.Write(data,0, data.Length);

newStream.Close();

request.CookieContainer=cc;

HttpWebResponse response=(HttpWebResponse)request.GetResponse();

cc.Add(response.Cookies);

Stream stream=response.GetResponseStream();stringresult=newStreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();returnresult;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值