请求和响应Cookie在控制台应用程序中不起作用

时间:2012-06-04 15:56:32

标签: c# cookies request console-application response

我有一个Windows控制台,当与用户进行HttpWebRequest时工作正常,但如果我尝试与其他用户一起使用,则无法正常工作。我认为问题出现在cookie中,因为当我使用不起作用的用户时,我获得的值越少,而不是使用有效的用户。

这是我的代码:

MainConsole:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var myManager = new RequestManager();


            myManager.SendPOSTRequest("http://www.example.com","", true);

            var myResponse = myManager.SendPOSTRequest("http://www.example.com/login.phtml", "login=UserName&pass=Password&action=login&%3E%3E+Login_x=33", true);


            var content = myManager.GetResponseContent(myResponse);

            Console.Write(content);
            Console.ReadLine();

        }
    }
}

RequestManager类:

namespace ConsoleApplication1
{
    public class RequestManager
    {
        public string LastResponse { protected set; get; }

        CookieContainer cookies = new CookieContainer();

        internal string GetCookieValue(Uri SiteUri, string name)
        {
            Cookie cookie = cookies.GetCookies(SiteUri)[name];
            return (cookie == null) ? null : cookie.Value;
        }

        public string GetResponseContent(HttpWebResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }
            Stream dataStream = null;
            StreamReader reader = null;
            string responseFromServer = null;

            try
            {
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                reader = new StreamReader(dataStream);
                // Read the content.
                responseFromServer = reader.ReadToEnd();
                // Cleanup the streams and the response.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (dataStream != null)
                {
                    dataStream.Close();
                }
                response.Close();
            }
            LastResponse = responseFromServer;
            return responseFromServer;
        }

        public HttpWebResponse SendPOSTRequest(string uri, string content, bool allowAutoRedirect)
        {
            HttpWebRequest request = GeneratePOSTRequest(uri, content,  allowAutoRedirect);
            return GetResponse(request);
        }

        public HttpWebResponse SendRequest(string uri, string content, string method, bool allowAutoRedirect)
        {
            HttpWebRequest request = GenerateRequest(uri, content, method, allowAutoRedirect);
            return GetResponse(request);
        }

        public HttpWebRequest GeneratePOSTRequest(string uri, string content, bool allowAutoRedirect)
        {
            return GenerateRequest(uri, content, "POST", allowAutoRedirect);
        }

        internal HttpWebRequest GenerateRequest(string uri, string content, string method, bool allowAutoRedirect)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            // Create a request using a URL that can receive a post. 
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            // Set the Method property of the request to POST.
            request.Method = method;
            // Set cookie container to maintain cookies
            request.CookieContainer = cookies;
            request.AllowAutoRedirect = allowAutoRedirect;

            if (method == "POST")
            {
                // Convert POST data to a byte array.
                byte[] byteArray = Encoding.UTF8.GetBytes(content);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
            }
            return request;
        }

        internal HttpWebResponse GetResponse(HttpWebRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)request.GetResponse();

                cookies.Add(response.Cookies);
                // Print the properties of each cookie.
                Console.WriteLine("\nCookies: ");
                foreach (Cookie cook in cookies.GetCookies(request.RequestUri))
                {
                    Console.WriteLine("Domain: {0}, String: {1}", cook.Domain, cook.ToString());
                }
            }
            catch (WebException ex)
            {
                Console.WriteLine("Web exception occurred. Status code: {0}", ex.Status);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return response;
        }
    }
}

编辑:我粘贴了我在Fiddler中可以看到的标题:)

不工作用户的标题:

Response sent 81 bytes of Cookie data:
    Set-Cookie: language=es_ES; expires=Tue, 05-Jun-2012 16:24:58 GMT; path=/; domain=.comunio.es

Response sent 30 bytes of Cookie data:
    Set-Cookie: session_language=es_ES; path=/

有效用户的标题:

Response sent 81 bytes of Cookie data:
    Set-Cookie: language=es_ES; expires=Tue, 05-Jun-2012 16:27:12 GMT; path=/; domain=.comunio.es

Response sent 30 bytes of Cookie data:
    Set-Cookie: session_language=es_ES; path=/

Response sent 190 bytes of Cookie data:
    Set-Cookie: phpbb2mysql_data=a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bs%3A7%3A%225638420%22%3B%7D; expires=Tue, 04-Jun-2013 16:27:12 GMT; path=/; domain=comunio.es

Response sent 75 bytes of Cookie data:
    Set-Cookie: phpbb2mysql_sid=e0e1f8e93e57afe267d240da4b596130; path=/; domain=comunio.es

Response sent 94 bytes of Cookie data:
    Set-Cookie: c=e2d6059d1f3a7871d99cc6a17883b198bf0f223d672e21b2fa9a16e8f9e74a63; path=/; domain=.comunio.es

0 个答案:

没有答案