HttpWebRequest 请求网页为什么拿不到ajax 返回cookiee

07:51 提问
HttpWebRequet模拟请求与浏览器一样,为什么Respnse回来的结果不一样?
各位大牛,
小弟业余爱好者。我用HttpWebRequest模拟请求网站,我通过,Fiddler查看
我与浏览器的请求头Header,内容一样,但返回的结果不一样,我的程序得到的
是空白内容,而浏览器返回的是真正的内容。我研究,并提取了cookie,重新加
入请求头再次请求,结果还是空,到底是什么原因啊。请求各位高人,指点迷津。
按赞数排序
换个网站试试呢,所有网站都一样吗?
除了cookie,还有其他http hjeader的数据是否一致
无非就是请求头的各个字段,比如useragent cookie refererx-forward-for等等以及url和post data
要特别注意,一些东西,比如url参数什么的,要根据请求生成,而未必是每次照抄。
打个比方,服务器返回一个验证码,你提交上去。这个过程是一次性的。因为服务器收到你的提交无论对错,都会清除/修改服务器session中的验证码了。
那么你再次用相同的请求伐过去,自然就无效了。所以还要结合你的程序来看。往往秘密还在你的页面的js代码中。
其他相似问题C# WebRequest发起Http Post请求模拟登陆并cookie处理示例
我的图书馆
C# WebRequest发起Http Post请求模拟登陆并cookie处理示例
C# WebRequest发起Http Post请求模拟登陆并cookie处理示例
CookieContainer cc=new CookieContainer();
string url = “http://mailbeta.263.net/xmweb”;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = “POST”;
request.ContentType = “application/x-www-form-urlencoded”; request.CookieContainer=
string user=”user”; //用户名
string pass=”pass”; //密码
string data = “func=login&usr=” + HttpUtility.UrlEncode(user) + “&sel_domain=263.net&domain=263.net&pass=” +
HttpUtility.UrlEncode(pass) +
“&image2.x=0&image2.y=0&verifypcookie=&verifypip=”;
request.ContentLength = data.L
StreamWriter writer = new
StreamWriter(request.GetRequestStream(),Encoding.ASCII); writer.Write(data);
writer.Flush();
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string encoding = response.ContentE
if (encoding == null || encoding.Length & 1) {
encoding = "UTF-8"; //默认编码
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
data = reader.ReadToEnd();
Console.WriteLine(data);
response.Close();
int index=data.IndexOf("sid=");
string sid=data.Substring(index+4,data.IndexOf("&",index)-index-4); Console.WriteLine(sid);
"http://wm11.263.net/xmweb?func=mlst&act=show&usr="+user+"&sid="+sid+"&fid=1&desc=1&pg=1&searchword=&searchtype=&searchsub=&searchfd=&sort=4";
request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer =
foreach(Cookie cookie in response.Cookies) {
cc.Add(cookie);
response = (HttpWebResponse)request.GetResponse();
encoding = response.ContentE
if (encoding == null || encoding.Length & 1) {
encoding = "UTF-8"; //默认编码
reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
data = reader.ReadToEnd();
Console.WriteLine(data);
response.Close();
这段代码的意思是,模拟登陆263邮箱,别列出收件箱内容(html代码) Related posts:
1. C# WebRequest处理Https请求
2. C# WebRequest处理Https请求之使用客户端证书
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&
喜欢该文的人也喜欢HttpWebRequest.CookieContainer 属性 (System.Net)
CookieContainer 属性
本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
HttpWebRequest.CookieContainer 属性
.NET Framework (current version)
获取或设置与此请求关联的 Cookie。命名空间:
System(位于 System.dll)
public virtual CookieContainer CookieContainer { get; set; }
The P:System.Net.HttpWebRequest.CookieContainer property provides an instance of the T:System.Net.CookieContainer class that contains the cookies associated with this request.CookieContainer is null by default. You must assign a
object to the property to have cookies returned in the
property of the
returned by the
method.说明 For security reasons, cookies are disabled by default. If you want to use cookies, use the P:System.Net.HttpWebRequest.CookieContainer property to enable cookies.The following code example sends a request to a URL and displays the cookies returned in the response.
using System.N
namespace Examples.System.Net.Cookies
// This example is run at the command line.
// Specify one argument: the name of the host to
// send the request to.
// If the request is sucessful, the example displays the contents of the cookies
// returned by the host.
public class CookieExample
public static void Main(string[] args)
if (args == null || args.Length != 1)
Console.WriteLine("Specify the URL to receive the request.");
Environment.Exit(1);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
// Print the properties of each cookie.
foreach (Cookie cook in response.Cookies)
Console.WriteLine("Cookie:");
Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
Console.WriteLine("Domain: {0}", cook.Domain);
Console.WriteLine("Path: {0}", cook.Path);
Console.WriteLine("Port: {0}", cook.Port);
Console.WriteLine("Secure: {0}", cook.Secure);
Console.WriteLine("When issued: {0}", cook.TimeStamp);
Console.WriteLine("Expires: {0} (expired? {1})",
cook.Expires, cook.Expired);
Console.WriteLine("Don't save: {0}", cook.Discard);
Console.WriteLine("Comment: {0}", ment);
Console.WriteLine("Uri for comments: {0}", mentUri);
Console.WriteLine("Version: RFC {0}" , cook.Version == 1 ? "2109" : "2965");
// Show the string representation of the cookie.
Console.WriteLine ("String: {0}", cook.ToString());
// Output from this example will be vary depending on the host name specified,
// but will be similar to the following.
CustomerID = 13xyz
Secure: False
When issued: 1/14/:57 PM
Expires: 1/17/:07 AM (expired? False)
Don't save: False
Uri for comments:
Version: RFC 2965
String: CustomerID = 13xyz
通用 Windows 平台
自 8 起可用
.NET Framework
自 1.1 起可用
可移植类库
Silverlight
自 3.0 起可用
Windows Phone Silverlight
自 7.0 起可用
Windows Phone
自 8.1 起可用
此页面有帮助吗?
更多反馈?
1500 个剩余字符
我们非常感谢您的反馈。1683人阅读
寒假开始的时候开始学C#,然后就试着用C#写个自动登入学校urp网站的程序,这样出了成绩直接发邮件告诉我,我就不用自己去一遍一遍登入查看了(听华科的同学说华科就是发邮件通知的,真人性化)
发邮件的话用System.Web.Mail.MailMessage mailmsg = new MailMessage();C#的话还要记得在项目的引用里添加程序集。
给mailmsg填写发件人的账号密码的时候要这么写:
mailmsg.Fields.Add(&/cdo/configuration/smtpauthenticate&, &1&);
mailmsg.Fields.Add(&/cdo/configuration/sendusername&, &username&);
mailmsg.Fields.Add(&/cdo/configuration/sendpassword&, &password&);
其它都比较好理解。
最后再设定邮件服务器并发送:就发送出去了。
SmtpMail.SmtpServer = &&;
SmtpMail.Send(mailmsg);
自动登入是用HttpWebRequest,具体方法可以参考
但是用上述方法对于有些网站会有一些问题,我碰到的主要是两方面。
1.一些网站会根据HttpWebRequest的referer来防止盗链,所以需要添加这么一条:
if (!string.IsNullOrEmpty(refer)) request.Referer =
2.网站是根据cookie来确定用户身份的话,登陆页面之后响应头里会拿到一个Set-Cookie,但是有时候明明已经登入了,响应头里的Set-Cookie却是空的。这是因为网页请求自动跳转了,返回的响应是跳转之后的响应,那时已经不需要返回Set-Cookie了。一个简单的解决办法是在request里禁用自动跳转:
request.AllowAutoRedirect =
拿到响应流之后,需要获取什么信息,用正则表达式去匹配一下就好了。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1731次
排名:千里之外

我要回帖

更多关于 ajax 返回cookie 的文章

 

随机推荐