Asp.net 通过HttpWebRequest从远程域获取cookie

Asp.net 通过HttpWebRequest从远程域获取cookie,asp.net,cookies,httpwebrequest,Asp.net,Cookies,Httpwebrequest,我必须在不同的域上创建页面:并且(实际上是http处理程序)。 通过WebHttpRequest,我将post请求从第1页发送到第2页 string result; var webRequest = (HttpWebRequest)WebRequest.Create("http://domain2/page2.aspx"); webRequest.Method = WebRequestMethods.Http.Post; webRequest.ContentType = "application

我必须在不同的域上创建页面:并且(实际上是http处理程序)。 通过WebHttpRequest,我将post请求从第1页发送到第2页

string result;
var webRequest = (HttpWebRequest)WebRequest.Create("http://domain2/page2.aspx");
webRequest.Method = WebRequestMethods.Http.Post;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = 0;
using (var webResponse = webRequest.GetResponse())
{
  if (webResponse == null)
    return null;
   var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8, true);    
   try
   {
     result = reader.ReadToEnd();
     if (string.IsNullOrEmpty(result))
       return null;
     }
   finally
   {
     reader.Close();
   }
 }
 return result.Deserialize();
我知道,domain2上有一个cookie,但当我进入page2.aspx时,cookies集合是空的。 当我做出简单的回应时。重定向到第2页,cookie存在。
那么,有可能提出这样的要求吗?我哪里做错了?或者有其他方法可以执行类似的操作?

您是通过代码而不是通过浏览器执行对第2页的请求。通常,它是一个将cookie(与站点关联)发送到web服务器的浏览器。在您的情况下,您需要在代码中将cookie名称值从page1的响应手动传输到page2的请求(假设这就是您想要/想要的意思)

此外,默认情况下,
HttpWebRequest
中禁用cookie,因此即使page2服务器代码正在添加一些cookie,也不会在响应对象中看到cookie,除非您使用属性启用它们(请阅读链接中的文档/示例)