0

httpwebreqest/httpwebresponse を使用していますが、一部のサイトで httpwebresponse が Cookie を認識しないという問題があります。これが 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= 

Raw (response.Headers からの Cookie はすべて 1 行の文字列です):

 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= 

次の正規表現は完全に機能します。

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

しかし問題は、ドメインと有効期限もスクレイピングする必要があることですが、ドメインと「有効期限」が混在する場所に表示されます。すべての Cookie をスクレイピングしてドメインと有効期限フィールドを取得するにはどうすればよいですか? ありがとう!

4

1 に答える 1

1

次のようなものが必要です。

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

以下のオプションで

RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture

時間がすべて GMT であるかどうかによっては、より洗練されたものを使用して「有効期限」の部分をキャプチャすることをお勧めします。

于 2013-10-30T08:55:13.867 に答える