如何在ASP.NET请求上传递cookie

时间:2011-12-30 14:47:21

标签: asp.net session cookies request

我需要将所有当前页面Cookie通过请求传递给其他服务器:

string url = "http://www.someserver.com/page1.aspx";
        // Create a request for the URL. 
        WebRequest request = WebRequest.Create(url);
        // If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials;
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams and the response.
        reader.Close();
        response.Close();

我应该在此代码中添加什么来读取当前Cookie并将其传递给http://www.someserver.com/page1.aspx

由于

1 个答案:

答案 0 :(得分:1)

((HttpWebRequest)request).Headers[HttpRequestHeader.Cookie] = 
    Request.Headers[HttpRequestHeader.Cookie.ToString()];

显然,此处使用的Request变量是ASP.NET HttpRequest实例。