做了一个模拟登录的但是一直不能把cookie携带进去,实在搞晕了,求助代码如下
[C#] 纯文本查看 复制代码
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem();
string cookie = "";
//自动使用
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = getImg();
}
//发送数据
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = gethtml();
}
//获取图片
private Image getImg()
{
item = new HttpItem()
{
URL = "http://127.0.0.1/validate.aspx?t="+DateTime.Now,
Method = "get",//URL 可选项 默认为Get
Timeout = 50000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
ContentType = "text/html",// 设置cookie类型
Accept = "*/*",
};
//返回cookie
HttpResult result = http.GetHtml(item);
string coo = result.Cookie;
coo = HttpHelper.GetSmallCookie(coo);
cookie = coo;
//变成图片
Image img = http.GetImage(item);
return img;
}
private string gethtml()
{
string coo = cookie;
//获取方框内的数值
string abc = textBox1.Text.Trim();
//创建Httphelper参数对象
item = new HttpItem()
{
URL = "http://127.0.0.1/json/dologinc.aspx?",//URL 必需项
Method = "post",//URL 可选项 默认为Get
ContentType = "application/x-www-form-urlencoded",
Timeout = 50000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = coo,
Referer = "http://127.0.0.1/bgim/account/login.htm", //来源
Postdata = "VerifyCode=" + abc + "name=admin&password=admin&isSec=0&cid=0&systemversion=4_4",//Post要发送的数据
PostDataType = PostDataType.String,
};
HttpResult result = http.GetHtml(item);
MessageBox.Show(result.Cookie, coo); //消息箱子打印出对比
string html = result.Html;
return html;
}
楼主是自学,如果有基础错误也麻烦指导一下.
按照我自己的想法 应该是 result.Cookie 跟coo应该是相同的,但是post过去的cookie一直都是错的 返回的也一直是验证码错误
|