httpwebrequest 标头中多个 Set-Cookie 的问题

发布于 2024-11-08 12:16:21 字数 1057 浏览 5 评论 0原文

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

当服务器响应多个“Set-Cookie:”标头时,只有 可供我

第一个在标头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

我期待您的回复,

提前致谢......

I try to login amazon.com using C# HTTPWebRequest class, i can login but not able to read multiple set-cookies in header

When the server responds with multiple "Set-Cookie:"-headers, only the first one becomes available to me in header

Web Response in HTTPWebRequest

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

How to get all "Set-Cookie:" in header using HTTPWebRequest class??

Web Response in HTTP Live Header

    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

I am looking forward your reply,

Thanks in advance.......

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2024-11-15 12:16:21

我认为您的意思是在 HttpWebResponse 对象中获取 Set-Cookie 标头;如果是这样,那么您可以使用 GetValues 方法来获取所有Set-Cookie 标头的实例。

    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);
                    }
                }
            }
        }
    }

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);
                    }
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文