2011-12-22 8 views
9

Yahoo Developer APIにプログラムでログインしようとしています。私は、HttpWebResponseの "Set-Cookie"ヘッダーのすべてにアクセスできない障害が発生しました。HttpWebResponseから複数の「Set-Cookie」ヘッダーを取得する

Set-Cookie: B=733jjvp7f5g8f&b=4&d=1pFN8bVpYFYaPUme88.fc6ZzTSI-&s=kc&i=.1p3Ei3yvwqZjo0gcg7D; expires=Sun, 22-Dec-2013 05:33:04 GMT; path=/; domain=.yahoo.com 
Set-Cookie: F=a=GYsABKAMvTZoTcNAPKUXrclX_Hb77EA7I_62nONz8QeEwNevHwqJ_NyizED88uhv9aMx.9o-&b=3tN5; expires=Sun, 22-Dec-2013 05:33:04 GMT; path=/; domain=.yahoo.com 
Set-Cookie: Y=v=1&n=0v251rt3ifppb&l=0kii84if0h70ma/o&p=m2fvvau012000000&iz=1111&r=if&lg=en-AU&intl=au&np=1; path=/; domain=.yahoo.com 
Set-Cookie: PH=fn=jW23i4lnq1UpiP.lsuU-&l=en-AU; expires=Sun, 22-Dec-2013 05:33:04 GMT; path=/; domain=.yahoo.com 
Set-Cookie: T=z=QEs8OBQYTBPBEZq31nTCqv1MzNPBjUwTjcwMDZOTjY-&a=YAE&sk=DAAtoxgrYmWIMk&ks=EAA3Ha0H7qyCT8P3cI9NWJrIA--~E&d=c2wBTkRRNEFUSTNPVEEzTnpFNU9URS0BYQFZQUUBZwFCRFZQTkRSSjJQRVRDTEdFT0xCQ1hER0VVUQFvawFaVzAtAXRpcAF2MkNUVUEBenoBUUVzOE9CQTdF; path=/; domain=.yahoo.com 
Set-Cookie: SSL=v=1&s=kTc532PQYAe1iT.23Q55E50ZdoOAdEK_fshc3g_YZ3SxszcbuHkmpJUAQ7RT67nDNA0nXyX68um90ZuS9RQztQ--&kv=0; path=/; domain=.yahoo.com; secure; httponly 

は、しかし、私は.NET経由「のSet-Cookie」の最初のインスタンスを超えて何かをアクセスできませんよ:

フィドラーは応答のヘッダに以下のクッキーを私に示して

// Make the web request: 
var userAuthWebRequest = WebRequest.Create(uri) as HttpWebRequest; 
var response = userAuthWebRequest.GetResponse() as HttpWebResponse; 

// Dump the headers to debug: 
Debug.WriteLine(string.Format("Set-Cookie: {0}", response.Headers.Get("Set-Cookie"))); 

私のデバッグ出力を返します:興味深いことに

Set-Cookie: B=733jjvp7f5g8f&b=3&s=b1; expires=Sun, 22-Dec-2013 05:33:03 GMT; path=/; domain=.yahoo.com 

、私は彼に似た要求を行う場合aders.GetValuesは、それが実際には、上記一緒に連結されているように見える「のSet-Cookie」ヘッダ、の2つのインスタンスを返します。

foreach (var headerName in response.Headers.AllKeys) 
{ 
    foreach (var values in response.Headers.GetValues(headerName)) 
    { 
     Debug.WriteLine("{0}: {1}", headerName, values); 
    } 
} 

出力:私はいくつかの他のを見てきました

Set-Cookie: B=733jjvp7f5g8f&b=3&s=b1; expires=Sun 
Set-Cookie: 22-Dec-2013 05:33:03 GMT; path=/; domain=.yahoo.com 

生のヘッダーがHttpWebResponseオブジェクトで利用できないことを確認する質問があり、ソケットソリューションを使用して調査する必要があります。私はFiddlerCoreをチェックして、そのことに幸運があるかどうかを確認しようとしていますが、誰かが他のポインタを持っていれば、それらを見たいと思っています。

ありがとうございます!

答えて

7

通常、'CookieCollection'クラスを使用して、設定されたCookieヘッダーの解析と処理を処理します。 M.Babcock @

using System.Net; 
using System; 
namespace Examples.System.Net.Cookies 
{ 
    // This example is run at the command line. 
    // Specify one argument: the name of the host to 
    // send the request to. 
    // If the request is sucessful, the example displays the contents of the cookies 
    // returned by the host. 

    public class CookieExample 
    { 
     public static void Main(string[] args) 
     { 
      if (args == null || args.Length != 1) 
      { 
       Console.WriteLine("Specify the URL to receive the request."); 
       Environment.Exit(1); 
      } 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]); 
      request.CookieContainer = new CookieContainer(); 

      HttpWebResponse response = (HttpWebResponse) request.GetResponse(); 



      // Print the properties of each cookie. 
      foreach (Cookie cook in response.Cookies) 
      { 
       Console.WriteLine("Cookie:"); 
       Console.WriteLine("{0} = {1}", cook.Name, cook.Value); 
       Console.WriteLine("Domain: {0}", cook.Domain); 
       Console.WriteLine("Path: {0}", cook.Path); 
       Console.WriteLine("Port: {0}", cook.Port); 
       Console.WriteLine("Secure: {0}", cook.Secure); 

       Console.WriteLine("When issued: {0}", cook.TimeStamp); 
       Console.WriteLine("Expires: {0} (expired? {1})", 
        cook.Expires, cook.Expired); 
       Console.WriteLine("Don't save: {0}", cook.Discard);  
       Console.WriteLine("Comment: {0}", cook.Comment); 
       Console.WriteLine("Uri for comments: {0}", cook.CommentUri); 
       Console.WriteLine("Version: RFC {0}" , cook.Version == 1 ? "2109" : "2965"); 

       // Show the string representation of the cookie. 
       Console.WriteLine ("String: {0}", cook.ToString()); 
      } 
     } 
    } 
} 
+3

乾杯:MSDN(HttpWebRequest.CookieContainer)から例をチェックアウトし、それを実装する方法のヘルプについて

。このアドバイスは、私にリクエスト/レスポンスオブジェクトの見直しをもたらしました。私が最初に見ていた200回の応答の前に私は302回の自動リダイレクト応答を得ていたことが分かります。クッキーは最初の302応答にありましたが、最終的な応答ではありませんでした。そのため、私は前にそれらを見ていませんでした。最初のリクエストで "AllowAutoRedirect = false"を設定すると、302の応答でCookieを適切に処理できるようになりました。 – Dakkith

+0

@Dakkith - HttpWebRequestオブジェクトで 'AllowAutoRedirect'プロパティをfalseに設定してリダイレクトを手動で処理し、応答のステータスコードを確認し、リダイレクトの場合は 'location'ヘッダーで指定されたアドレスに初期リクエストを再送信できます応答の。 –

関連する問題