HttpWebRequest编程相关问题(4)
2007-07-23 23:31
585 查看
第一个问题还是同Cookies相关,同Cookies的存储有很多关系,以CSDN为例,可能存在下面两个Cookies
1,Domain=".writeblog.csdn.net" ;
2,Domain="writeblog.csdn.net";
值得注意的是这是两个不同的Domain.但是如果两个Cookie的Name一样的话如何办?
当然浏览器有自己的办法,我们不去深究,但是我们用HttpWebRequest提交Cookies上去的时候怎么办?
Cookie名称相同,Domain不同,我们提交那个呢?
呵呵,其实俺也不知道,只是俺的解决办法是有的,我这里使用起来没有问题,但是不保证换个地方不出错。
待会写出代码来。
第二个问题也是害死人。我今天一天又耗费了无数的脑细胞啊!!!!
有的网站使用了ajax技术。我们捕获到的HttpHeader如下:
POST /control/doPostDiary.b HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Referer: http://xxxx.xxx.com/control/diary/postDiary.b
Content-Type: text/xml;charset=GBK
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; WOW64; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Host: dragonya.bokee.com
Content-Length: 1621
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: xxxx
<这里是个xml字符串>
当用httpwebrequest模拟这个请求时,俺再次花了无数的脑细胞都没有办法解决。
我一直以为我在模拟浏览器请求的时候是不是少传递了Cookie?
还是jspsessionid又在不停的变化???找了无数的原因和方式
反正一个下午没有解决,一直到晚上不甘心才发觉问题所在:
Post方式传递数据是Content-Type=application/x-www-form-urlencoded
而那个ajax提交数据时Content-Type: text/xml;charset=GBK
这是一个小小的,根本想不到的问题,对于post方式,我们的思维已经固化了“application/x-www-form-urlencoded”,想不到也不知道有这种情况。
为什么是我在摸着石头过河啊?为什么没有别人摸着石头过了河,然后弄一座桥呢???
最新的代码如下:
public struct SendHead
...{
public string Host;
public string Referer;
public CookieCollection Cookies;
public string Action;
public string PostData;
public string Method;
public string ContentType;
public string Html;
}
public class Http
...{
public CookieCollection Cookies = null;
public string Send(ref SendHead oHead)
...{
HttpWebResponse oResponse=null;
HttpWebRequest oRequest = (HttpWebRequest)HttpWebRequest.Create(oHead.Host+ oHead.Action);
oRequest.ProtocolVersion = new Version("1.1");
oRequest.Referer = oHead.Referer;
oRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*";
oRequest.CookieContainer = new CookieContainer();
oRequest.Timeout = 30000;
if (oHead.Cookies != null)
...{
string cookiesValue = "";
foreach (Cookie o in oHead.Cookies)
...{
cookiesValue += o.Name+"="+ o.Value+",";
}
oRequest.CookieContainer.SetCookies(new Uri(oHead.Host), cookiesValue);
}
//oRequest.CookieContainer.SetCookies(oRequest.RequestUri,cookies[].
oRequest.UserAgent = "NutsSoft.com.cn";
oRequest.Credentials = CredentialCache.DefaultCredentials;
//设置POST数据
if (oHead.Method.ToLower() == "post")
...{
oRequest.Method = "POST";
byte[] byteData = ASCIIEncoding.ASCII.GetBytes(oHead.PostData);
if (string.IsNullOrEmpty(oHead.ContentType))
...{
oRequest.ContentType = "application/x-www-form-urlencoded";
}
else
...{
oRequest.ContentType = oHead.ContentType;
}
oRequest.ContentLength = byteData.Length;
Stream WriteStream = oRequest.GetRequestStream();
WriteStream.Write(byteData, 0, byteData.Length);
WriteStream.Close();
}
try
...{
oResponse = (HttpWebResponse)oRequest.GetResponse();
}
catch (WebException ex)
...{
if (ex.Response != null)
...{
oResponse = (HttpWebResponse)ex.Response;
}
else
...{
throw ex;
}
}
//oResponse.Cookies.Add(oRequest.CookieContainer.GetCookies(oRequest.RequestUri));
CookieCollection tmpCookies = oRequest.CookieContainer.GetCookies(oRequest.RequestUri);
foreach (Cookie o in tmpCookies)
...{
if (oResponse.Cookies[o.Name] != null)
...{
// oResponse.Cookies[o.Name].Value = o.Value;
}
else
...{
oResponse.Cookies.Add(o);
}
}
Stream dataStream = oResponse.GetResponseStream();
string en = oResponse.CharacterSet;
if (en == null) en = "gb2312";
if (en.ToLower() == "iso-8859-1") en = "gb2312";
StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding(en));
string responseFromServer = reader.ReadToEnd();
Cookies=oResponse.Cookies;
oHead.Cookies =oResponse.Cookies;
reader.Close();
dataStream.Close();
oResponse.Close();
return oHead.Html=responseFromServer;
}
如果你打算使用上面的代码,或者认为有帮助的话,江湖规矩:回一帖三!!!
如果要用于商业产品请Email通知到Sean.Pu@gmail.com. 谢谢您的合作。
PS,到现在为止,已经完成如下博客的迁移服务:Blogcn,Blogbus,sohu,IIU,Bokee,提供所有受支持博客之间的互相迁入和迁出,还在加入其他模版。如有相关意愿请联系我。
1,Domain=".writeblog.csdn.net" ;
2,Domain="writeblog.csdn.net";
值得注意的是这是两个不同的Domain.但是如果两个Cookie的Name一样的话如何办?
当然浏览器有自己的办法,我们不去深究,但是我们用HttpWebRequest提交Cookies上去的时候怎么办?
Cookie名称相同,Domain不同,我们提交那个呢?
呵呵,其实俺也不知道,只是俺的解决办法是有的,我这里使用起来没有问题,但是不保证换个地方不出错。
待会写出代码来。
第二个问题也是害死人。我今天一天又耗费了无数的脑细胞啊!!!!
有的网站使用了ajax技术。我们捕获到的HttpHeader如下:
POST /control/doPostDiary.b HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Referer: http://xxxx.xxx.com/control/diary/postDiary.b
Content-Type: text/xml;charset=GBK
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; WOW64; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
Host: dragonya.bokee.com
Content-Length: 1621
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: xxxx
<这里是个xml字符串>
当用httpwebrequest模拟这个请求时,俺再次花了无数的脑细胞都没有办法解决。
我一直以为我在模拟浏览器请求的时候是不是少传递了Cookie?
还是jspsessionid又在不停的变化???找了无数的原因和方式
反正一个下午没有解决,一直到晚上不甘心才发觉问题所在:
Post方式传递数据是Content-Type=application/x-www-form-urlencoded
而那个ajax提交数据时Content-Type: text/xml;charset=GBK
这是一个小小的,根本想不到的问题,对于post方式,我们的思维已经固化了“application/x-www-form-urlencoded”,想不到也不知道有这种情况。
为什么是我在摸着石头过河啊?为什么没有别人摸着石头过了河,然后弄一座桥呢???
最新的代码如下:
public struct SendHead
...{
public string Host;
public string Referer;
public CookieCollection Cookies;
public string Action;
public string PostData;
public string Method;
public string ContentType;
public string Html;
}
public class Http
...{
public CookieCollection Cookies = null;
public string Send(ref SendHead oHead)
...{
HttpWebResponse oResponse=null;
HttpWebRequest oRequest = (HttpWebRequest)HttpWebRequest.Create(oHead.Host+ oHead.Action);
oRequest.ProtocolVersion = new Version("1.1");
oRequest.Referer = oHead.Referer;
oRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*";
oRequest.CookieContainer = new CookieContainer();
oRequest.Timeout = 30000;
if (oHead.Cookies != null)
...{
string cookiesValue = "";
foreach (Cookie o in oHead.Cookies)
...{
cookiesValue += o.Name+"="+ o.Value+",";
}
oRequest.CookieContainer.SetCookies(new Uri(oHead.Host), cookiesValue);
}
//oRequest.CookieContainer.SetCookies(oRequest.RequestUri,cookies[].
oRequest.UserAgent = "NutsSoft.com.cn";
oRequest.Credentials = CredentialCache.DefaultCredentials;
//设置POST数据
if (oHead.Method.ToLower() == "post")
...{
oRequest.Method = "POST";
byte[] byteData = ASCIIEncoding.ASCII.GetBytes(oHead.PostData);
if (string.IsNullOrEmpty(oHead.ContentType))
...{
oRequest.ContentType = "application/x-www-form-urlencoded";
}
else
...{
oRequest.ContentType = oHead.ContentType;
}
oRequest.ContentLength = byteData.Length;
Stream WriteStream = oRequest.GetRequestStream();
WriteStream.Write(byteData, 0, byteData.Length);
WriteStream.Close();
}
try
...{
oResponse = (HttpWebResponse)oRequest.GetResponse();
}
catch (WebException ex)
...{
if (ex.Response != null)
...{
oResponse = (HttpWebResponse)ex.Response;
}
else
...{
throw ex;
}
}
//oResponse.Cookies.Add(oRequest.CookieContainer.GetCookies(oRequest.RequestUri));
CookieCollection tmpCookies = oRequest.CookieContainer.GetCookies(oRequest.RequestUri);
foreach (Cookie o in tmpCookies)
...{
if (oResponse.Cookies[o.Name] != null)
...{
// oResponse.Cookies[o.Name].Value = o.Value;
}
else
...{
oResponse.Cookies.Add(o);
}
}
Stream dataStream = oResponse.GetResponseStream();
string en = oResponse.CharacterSet;
if (en == null) en = "gb2312";
if (en.ToLower() == "iso-8859-1") en = "gb2312";
StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding(en));
string responseFromServer = reader.ReadToEnd();
Cookies=oResponse.Cookies;
oHead.Cookies =oResponse.Cookies;
reader.Close();
dataStream.Close();
oResponse.Close();
return oHead.Html=responseFromServer;
}
如果你打算使用上面的代码,或者认为有帮助的话,江湖规矩:回一帖三!!!
如果要用于商业产品请Email通知到Sean.Pu@gmail.com. 谢谢您的合作。
PS,到现在为止,已经完成如下博客的迁移服务:Blogcn,Blogbus,sohu,IIU,Bokee,提供所有受支持博客之间的互相迁入和迁出,还在加入其他模版。如有相关意愿请联系我。
相关文章推荐
- HttpWebRequest编程相关问题(5)
- HttpWebRequest编程相关问题(2)
- HttpWebRequest编程相关问题(1)
- HttpWebRequest编程相关问题(3)
- HttpWebRequest 编程相关问题
- HttpWebRequest编程相关问题(6)
- HttpWebRequest编程相关问题阶段总结
- HttpWebRequest使用问题
- System.Net.HttpWebRequest.GetRequestStream超时问题
- 解决WebClient或HttpWebRequest首次连接缓慢问题
- C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例
- 【转】System.Net.HttpWebRequest请求超时的问题解决方法
- 解决WebClient或HttpWebRequest首次连接缓慢问题
- C# 使用HttpWebRequest Post提交数据,携带Cookie和相关参数示例
- 解决.net webservice的WebClient或HttpWebRequest首次连接缓慢问题
- C#中HttpWebRequest性能问题
- HttpWebRequest 首次连接缓慢问题
- HttpWebRequest多线程性能问题,请求超时的错误
- C# httpwebrequest post 发送的数据 urlencode 问题
- HttpWebRequest提高效率之连接数,代理,自动跳转,gzip请求等设置问题