有关发送http请求的问题

Anew_G 2012-03-04 02:16:41
原本我使用这段代码获取某地址的源代码



HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.url);

request.Timeout = 150000;

request.Headers.Set("Pragma", "no-cache");

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

Stream streamReceive = response.GetResponseStream();

Encoding encoding = Encoding.GetEncoding("GB2312");

StreamReader streamReader = new StreamReader(streamReceive, encoding);

strResult = streamReader.ReadToEnd();

streamReceive.Close();

streamReader.Close();



后来发现有些地址要用户登入后具有特殊权限的用户才能访问到

假设我在本地已经使用浏览器登入了具有相关权限的帐号

那是不是应该在请求的时候附带上本地相关网址的cookies,服务器才会返回我需要的源代码给我?
如果是的话,应该怎么做?
...全文
93 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Anew_G 2012-03-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 net_lover 的回复:]
string UserName = "xx";
string Password = "yy";
Encoding encoding = Encoding.GetEncoding("GB2312"); //编码根据接收页面不同可能不同
string postData = "UserName=" + UserName + "&Password=" + Password;//表单名称根据l……
[/Quote]

多谢了
johnwanzhi 2012-03-04
  • 打赏
  • 举报
回复

private string MopInfo()
{
string str = "";

string url = "http://dzh.mop.com/leftListData.do?page=0&rowNum=100&order=lastReply&mainplate=0";

string html = "";
Uri uri = new Uri(url);
try
{
HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
req.Timeout = 30000;
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
html = sr.ReadToEnd();//网页源代码
sr.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "请重试!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return str;
}
孟子E章 2012-03-04
  • 打赏
  • 举报
回复

string UserName = "xx";
string Password = "yy";
Encoding encoding = Encoding.GetEncoding("GB2312"); //编码根据接收页面不同可能不同
string postData = "UserName=" + UserName + "&Password=" + Password;//表单名称根据login.php页面填写
string strUrl = "http://xxx.com/login.phpx";
byte[] data = encoding.GetBytes(postData);
// 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// 发送数据
newStream.Write(data,0,data.Length);
newStream.Close();
......
Anew_G 2012-03-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 anew_g 的回复:]

引用 5 楼 net_lover 的回复:

HttpWebRequest 就是模仿浏览器,但不是浏览器,你能读取出电脑的Cookie也可以。但你这里不是浏览器,你设置了也没法发送到服务器的,你必须自己写代码发送Cookie信息,上面的例子有

你还可以搜索 asp.net 验证 HttpWebRequest


http://www.google.com/search?q=asp……
[/Quote]

我现在就想知道你代码里indata那该怎么填 对方网站是php的 我应该需要把帐号密码都放包里post过去它才会返回需要的cookies给我吧
Anew_G 2012-03-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 net_lover 的回复:]

HttpWebRequest 就是模仿浏览器,但不是浏览器,你能读取出电脑的Cookie也可以。但你这里不是浏览器,你设置了也没法发送到服务器的,你必须自己写代码发送Cookie信息,上面的例子有

你还可以搜索 asp.net 验证 HttpWebRequest


http://www.google.com/search?q=asp.net+%E9%AA%8C%E8%AF%81+……
[/Quote]

这次刚好比较急 又是第一次接触这个 看你给的例子有点头大 一句一句讲估计讲不清楚而且太耗时了 加Q吧 我分全部给你 附加人民币都行啊。。
ojekleen 2012-03-04
  • 打赏
  • 举报
回复
可以使用浏览器控件,直接访问登录页面,登录后再用 之类的代码采集。当然要加上cookie
孟子E章 2012-03-04
  • 打赏
  • 举报
回复
HttpWebRequest 就是模仿浏览器,但不是浏览器,你能读取出电脑的Cookie也可以。但你这里不是浏览器,你设置了也没法发送到服务器的,你必须自己写代码发送Cookie信息,上面的例子有

你还可以搜索 asp.net 验证 HttpWebRequest


http://www.google.com/search?q=asp.net+%E9%AA%8C%E8%AF%81+HttpWebRequest+&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:zh-CN:official&client=firefox-a
Anew_G 2012-03-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 net_lover 的回复:]

不一定需要知道服务器端代码,但表单名称必须一样
[/Quote]

那就需要该特殊权限账户的帐号密码咯?

假设我知道了帐号密码,那indata里的应该怎么填?

用户咋本地电脑已经用浏览器登入过的话,不能直接用已经在电脑里的cookies么?
孟子E章 2012-03-04
  • 打赏
  • 举报
回复
不一定需要知道服务器端代码,但表单名称必须一样
Anew_G 2012-03-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 net_lover 的回复:]

使用CookieContainer
参见
http://www.cnblogs.com/gotolnc/archive/2009/08/11/1543870.html
[/Quote]

// <%

// if request("aa")="zhuye" then session("ok")="ok"

// if session("ok")="ok" then

// response.write("登录")

// else

// response.write("没有登录")

// end if

// %>

这段是表示我需要知道服务器端的代码么?
孟子E章 2012-03-04
  • 打赏
  • 举报
回复
使用CookieContainer
参见
http://www.cnblogs.com/gotolnc/archive/2009/08/11/1543870.html

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧