HttpWebRequest编程相关问题(4)

本文探讨了Cookies在不同域下的处理方法及通过HttpWebRequest提交时的选择策略。此外,还解决了利用Ajax技术提交XML数据时遇到的Content-Type问题,并提供了一段实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一个问题还是同Cookies相关,同Cookies的存储有很多关系,以优快云为例,可能存在下面两个Cookies

1,Domain=".writeblog.youkuaiyun.com" ;

2,Domain="writeblog.youkuaiyun.com";

值得注意的是这是两个不同的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,提供所有受支持博客之间的互相迁入和迁出,还在加入其他模版。如有相关意愿请联系我。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值