关于HttpWebResponse的问题
首先要承认我的HttpWebResponse比较菜,很多东西事实而非,大家不要拍砖,手下留情,多多答疑解惑,多谢本人想做一个集登入、自动刷新和购买一条龙的外挂软件,但是HttpWebResponse实在是头次接触,感觉很不会用,碰到些问题,请大大们解惑。
1、在post的时候,__VIEWSTATE和__EVENTVALIDATION,需要赋值吗?
2、cookieString返回的内容是“ASP.NET_SessionId=tfnqvc454uewzev4nwlp2ume; path=/; HttpOnly”代表我登入成功了吗?
3、怎么将string cookieString 转换成 CookieCollection cookies ?
附我的源代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Xiaowa;
using System.Text.RegularExpressions;
namespace 网络精灵
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string loginUrl = textBox1.Text;
string userName = "xiaowa10";
string password = "xiaowa13";
string keeptime = "10800";
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$txtUserName", userName);
parameters.Add("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$txtPwd", password);
parameters.Add("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$ddlKeepTime", keeptime);
parameters.Add("$hfBroswerVersion", "8.0");
parameters.Add("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$btnLogin", "登 录");
parameters.Add("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$hfpwd", textBox2.Text.ToString());
parameters.Add("__VIEWSTATE", textBox3.Text.ToString());
parameters.Add("__EVENTVALIDATION", textBox4.Text.ToString());
//登入页面
HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, Encoding.UTF8, null);
//保存cookie
cookieString = response.Headers["Set-Cookie"];
//取得网页内容
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
//StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
string content = sr.ReadToEnd(); //得到访问页面信息源代码
response.Close();
richTextBox1.Text = content;
richTextBox2.Text = "读取页面成功\n" + richTextBox2.Text;
richTextBox2.Text = "cookies:" + cookieString + "\n" + richTextBox2.Text;
}
catch (Exception ex)
{
richTextBox2.Text = ex.Message.ToString() + "\n" + richTextBox2.Text;
}
}
public string cookieString = null;
private void button2_Click(object sender, EventArgs e)
{
try
{
CookieCollection cookies = new CookieCollection(); //如何从response.Headers["Set-Cookie"];中获取并设置CookieCollection的代码略
cookies = isgood.strCokAddCol(cookieString, textBox1.Text.ToString());
HttpWebResponse response = HttpWebResponseUtility.CreateGetHttpResponse(textBox1.Text.ToString(), null, null, cookies);
//取得网页内容
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
//StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
string content = sr.ReadToEnd(); //得到访问页面信息源代码
response.Close();
richTextBox1.Text = content;
richTextBox2.Text = "读取页面成功\n" + richTextBox2.Text;
}
catch (Exception ex)
{
richTextBox2.Text = ex.Message.ToString() + "\n" + richTextBox2.Text;
}
}
}
}
--------------------编程问答-------------------- 附代码所用类,本来想贴,太长贴不上来,就发我参考的页面也一样
http://blog.csdn.net/zhoufoxcn/article/details/6404236
--------------------编程问答-------------------- 自己顶下 --------------------编程问答-------------------- 坦率地说,对编程一窍不通没有什么可以指责的,但是你真的不应该省两个钱,使用现成的软件,或者请人开发一个有什么不值得的呢?除非你的需求本身就没有价值——那样的话,你更不应该自己研究。 --------------------编程问答--------------------
当然需需要赋值。想搞成一个应用程序,关键地是你时刻知道业务需求,这样才不会因为沉浸在一点编程语句的学习中而随意忽略业务需求(随然这很怪,但是许多人确实是学了一点编程就反而变懒了)。所以不用问是否要赋值的问题,只要这是业务逻辑上必须依赖的属性,那么根本无法想象不去赋值的问题。
如果对方是asp.net应用程序,那么对方通常都会返回sessionid给客户端,这样客户端浏览器下一次提交数据时再次提交这个值时服务器就知道是哪一个Session。这是asp.net应用程序自动完成的Session机制管理操作。跟对方的应用程序中是怎么处理“登入成功”并没有直接关系。就好象你拿起电话给人打电话拜年,对方知道你hold住了一个电话线,你确立的会话授权存在,但是不代表着对方就一定要认识你是谁。
第三个问题,你可以搜索一下httpwebrequest使用cookiecontainer对象的文章。我猜至少有一百万个搜索结果。 --------------------编程问答-------------------- 自己再顶下 --------------------编程问答-------------------- 1.需要
2.不代表你登录成功,只是表示你成功提交了请求,asp.net分配给你一个session ID用于辨别不同会话。
3.
string cookieString,cookieName;
//你的代码,赋值给cookieString,cookieName。
CookieCollection cookies=new CookieCollection();
cookies.Add(new cookie(cookieName,cookieString));
可以参考:
http://msdn.microsoft.com/zh-cn/library/z2yaeayk.aspx --------------------编程问答-------------------- 上面代码最后一行应该是:
cookies.Add(new Cookie(cookieName,cookieString)); --------------------编程问答-------------------- HttpWebResponse myReponse = (HttpWebResponse)myRequest.GetResponse();
补充:.NET技术 , C#