2013-10-30 44 views
0

我使用的是httpwebreqest/httpwebresponse,問題出在一些網站上,httpwebresponse不能識別cookie。這就是迴應。頭像返回。正則表達式來處理cookies

Cookie1=1;domain=subdomain.host.com;path=/;Expires=Thu, 30-Oct-1980 16:00:00 GMT 
Cookie2= ; HTTPOnly= ; domain=subdomain.host.com;path=/;Expires=Thu, 30-Oct-1980 16:00:00 GMT, 
Cookie5= ; domain=.host.com;path=/;HTTPOnly= ;version=1 
Cookie3=2; expires=Thu, 30-Oct-1980 16:00:00 GMT;domain=.host.com;path=/;HTTPOnly= ;version=1 
Cookie4=3; domain=.host.com;path=/;version= 

原(從response.Headers餅乾都在單行字符串):

Cookie1=1;domain=subdomain.host.com;path=/;Expires=Thu, 30-Oct-1980 16:00:00 GMT,Cookie2= ; HTTPOnly= ; domain=subdomain.host.com;path=/;Expires=Thu, 30-Oct-1980 16:00:00 GMT,Cookie5= ; domain=.host.com;path=/;HTTPOnly= ;version=1,Cookie3=2; expires=Thu, 30-Oct-1980 16:00:00 GMT;domain=.host.com;path=/;HTTPOnly= ;version=1,Cookie4=3; domain=.host.com;path=/;version= 

下面的正則表達式將很好地工作:

(.*?)=(.*?); 

但問題是我需要的颳去域名和截止日期,但域名和「過期」出現在混合位置。我如何刮取所有的cookies並獲得域名和過期域?謝謝!

+0

你想提取域並從每個cookie過期? –

+0

是的,以及cookie的名稱和價值。我不確定是否需要使用多個正則表達式或差異方法 – user2320462

回答

0

你需要的東西如下:

@"Cookie(?<index>\d+)\s*=\s*((domain\s*=\s*(?<domain>.*?)[;,])|(expires\s*=\s*(?<expires>.*?GMT))|(.(?!Cookie\d+=)))*" 

與下列選項

RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture 

根據您的時間是否都GMT,您可能需要使用一些更復雜的捕捉到了「過期'部分。