3

我尝试使用 C#HTTPWebRequest类登录 amazon.com,我可以登录但无法在标题中读取多个 set-cookies

当服务器响应多个“Set-Cookie:”-headers 时,只有第一个在 header 中对我可用

HTTPWebRequest 中的 Web 响应

    Set-Cookie: x-main=Yvi4723B?Nk5zuPPCZ0?66eGEI5kVnOM; path=/; domain=.amazon.com; 
expires=Wed May 25 13:20:05 2011 GMT

如何使用HTTPWebRequest类在标头中获取所有“Set-Cookie:”?

HTTP Live 标头中的 Web 响应

    Set-Cookie: x-main=Yvi4723B?Nk5zuPPCZ0?66eGEI5kVnOM; path=/; domain=.amazon.com; 
expires=Wed May 25 13:20:05 2011 GMT

Set-Cookie:atmain=4|WzNBbofyvkTvUaivgd9aS0Tzy0V2MJoUy+gJVcZ4szMhpt+gEyrRyLfUEodtZBKdn9vyJQJ3szoLgO12SUrB5XYqJ/tggOPurUepz5qsd6eg2V6vfbE0X1HQKp03xVkYLPEH5MDhmkMevQgkv36FyY+zA6HN5LzlM92+4kGu6wHeIILZ5+y/dtoYU/uORs1hC9hEo5iwP8Mljg4hDx7b/g==; path=/; domain=.amazon.com; expires=Wed May 25 13:20:05 2011 GMT; secure

Set-Cookie: ubid-main=182-3549292-6045052; path=/; domain=.amazon.com; expires=Wed May 25 13:20:05 2011 GMT

我期待着你的回复,

提前致谢.......

4

1 回答 1

0

I think you mean to get the Set-Cookie headers in the HttpWebResponse object; if so, then you can use the GetValues method to get the all the instances of the Set-Cookie header.

    public class StackOverflow_6045579_751090
    {
        static AutoResetEvent serverStarted = new AutoResetEvent(false);
        public static void Test()
        {
            new Thread(new ThreadStart(StartServer)).Start();
            serverStarted.WaitOne();
            SendRequest();
        }
        static void StartServer()
        {
            TcpListener serverSocket = new TcpListener(8000);
            TcpClient clientSocket = null;
            serverSocket.Start();
            serverStarted.Set();
            clientSocket = serverSocket.AcceptTcpClient();
            var clientStream = clientSocket.GetStream();
            byte[] bytesFrom = new byte[10025];
            int bytesRead = clientStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
            string dataFromClient = Encoding.UTF8.GetString(bytesFrom, 0, bytesRead);
            Console.WriteLine(" >> Data from client - " + dataFromClient);
            string serverResponse = @"HTTP/1.1 200 OK
Content-Length: 0
Set-Cookie: x-main=Yvi4723B?Nk5zuPPCZ0?66eGEI5kVnOM; path=/; domain=.amazon.com; expires=Wed May 25 13:20:05 2011 GMT
Set-Cookie:atmain=4|WzNBbofyvkTvUaivgd9aS0Tzy0V2MJoUy+gJVcZ4szMhpt+gEyrRyLfUEodtZBKdn9vyJQJ3szoLgO12SUrB5XYqJ/tggOPurUepz5qsd6eg2V6vfbE0X1HQKp03xVkYLPEH5MDhmkMevQgkv36FyY+zA6HN5LzlM92+4kGu6wHeIILZ5+y/dtoYU/uORs1hC9hEo5iwP8Mljg4hDx7b/g==; path=/; domain=.amazon.com; expires=Wed May 25 13:20:05 2011 GMT; secure
Set-Cookie: ubid-main=182-3549292-6045052; path=/; domain=.amazon.com; expires=Wed May 25 13:20:05 2011 GMT
Connection: close
Date: Mon, 23 May 2011 06:56:18 GMT

";
            Byte[] sendBytes = Encoding.UTF8.GetBytes(serverResponse);
            clientStream.Write(sendBytes, 0, sendBytes.Length);
            clientStream.Flush();
            clientStream.Close();
            clientSocket.Close();
        }
        public static void SendRequest()
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://" + Environment.MachineName + ":8000/foo");
            req.Method = "GET";
            HttpWebResponse resp;
            try
            {
                resp = (HttpWebResponse)req.GetResponse();
            }
            catch (WebException e)
            {
                resp = (HttpWebResponse)e.Response;
            }

            if (resp != null)
            {
                Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
                foreach (var headerName in resp.Headers.AllKeys)
                {
                    foreach (var values in resp.Headers.GetValues(headerName))
                    {
                        Console.WriteLine("{0}: {1}", headerName, values);
                    }
                }
            }
        }
    }
于 2011-05-23T07:12:50.710 回答