HttpWebRequest编程相关问题(4)

第一个问题还是同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
... {
publicstringHost;
publicstringReferer;
publicCookieCollectionCookies;
publicstringAction;
publicstringPostData;
publicstringMethod;
publicstringContentType;
publicstringHtml;
}

public class Http
... {
publicCookieCollectionCookies=null;

publicstringSend(refSendHeadoHead)
...{
HttpWebResponseoResponse
=null;
HttpWebRequestoRequest
=(HttpWebRequest)HttpWebRequest.Create(oHead.Host+oHead.Action);
oRequest.ProtocolVersion
=newVersion("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
=newCookieContainer();
oRequest.Timeout
=30000;
if(oHead.Cookies!=null)
...{
stringcookiesValue="";
foreach(CookieoinoHead.Cookies)
...{
cookiesValue
+=o.Name+"="+o.Value+",";
}

oRequest.CookieContainer.SetCookies(
newUri(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;
StreamWriteStream
=oRequest.GetRequestStream();
WriteStream.Write(byteData,
0,byteData.Length);
WriteStream.Close();
}

try
...{
oResponse
=(HttpWebResponse)oRequest.GetResponse();
}

catch(WebExceptionex)
...{
if(ex.Response!=null)
...{
oResponse
=(HttpWebResponse)ex.Response;
}

else
...{
throwex;
}

}

//oResponse.Cookies.Add(oRequest.CookieContainer.GetCookies(oRequest.RequestUri));
CookieCollectiontmpCookies=oRequest.CookieContainer.GetCookies(oRequest.RequestUri);
foreach(CookieointmpCookies)
...{
if(oResponse.Cookies[o.Name]!=null)
...{
//oResponse.Cookies[o.Name].Value=o.Value;
}

else
...{
oResponse.Cookies.Add(o);
}


}

StreamdataStream
=oResponse.GetResponseStream();

stringen=oResponse.CharacterSet;
if(en==null)en="gb2312";
if(en.ToLower()=="iso-8859-1")en="gb2312";
StreamReaderreader
=newStreamReader(dataStream,Encoding.GetEncoding(en));
stringresponseFromServer=reader.ReadToEnd();
Cookies
=oResponse.Cookies;
oHead.Cookies
=oResponse.Cookies;
reader.Close();
dataStream.Close();
oResponse.Close();
returnoHead.Html=responseFromServer;
}

如果你打算使用上面的代码,或者认为有帮助的话,江湖规矩:回一帖三!!!

如果要用于商业产品请Email通知到[email protected]. 谢谢您的合作。

PS,到现在为止,已经完成如下博客的迁移服务:Blogcn,Blogbus,sohu,IIU,Bokee,提供所有受支持博客之间的互相迁入和迁出,还在加入其他模版。如有相关意愿请联系我。

你可能感兴趣的:(request)