简体   繁体   English

C#HttpWebRequest Cookie未传输

[英]C# HttpWebRequest Cookies not transmitted

I'm trying to transmit two manually created Cookies with a initial request from the client to a server (no response available, where the Cookies could be extracted) using the following code (as discussed eg here ) 我试图用从客户机到服务器 (没有可用的响应,其中,所述饼干可以提取)一个初始请求传输两个手动创建的Cookie使用以下代码(如所讨论的例如此处

HttpWebRequest request = 
      (HttpWebRequest)HttpWebRequest.Create(
      "https://intra.group.net:5115/app/search.do");

//...

CookieContainer oCookies = new CookieContainer();
CookieCollection cookieGroup = new CookieCollection();
Cookie c1 = new Cookie("name1", "value1", "/", ".intra.group.net");
Cookie c2 = new Cookie("name2", "value2", "/", ".intra.group.net");
cookieGroup.Add(c1);
cookieGroup.Add(c2);
oCookies.Add(cookieGroup);
request.CookieContainer = oCookies;

But raw communication doesn't contain (according to Fiddler) any Cookie data. 但是原始通信不包含(根据Fiddler)任何Cookie数据。 Any suggestions why they are missing? 有什么建议为什么他们不见了? Thank you very much in advance! 提前非常感谢您!

Maybe it's some synchronize problem. 也许是一些同步问题。 Sometimes the problem is the HttpOnly cookies that are missing from Document.Cookie for security reasons.You can try this solution here . 有时,由于安全原因,Document.Cookie中缺少HttpOnly cookie。您可以在此处尝试此解决方案。

Cookies are received in the HttpWebRequest, not sent. Cookies在HttpWebRequest中接收,未发送。

They are Sent with the WebReponse. 它们通过WebReponse发送。

-> HttpContext.Current.Request : Contains Received Cookies -> HttpContext.Current.Request:包含收到的Cookie

-> HttpContext.Current.Response: Contains cookies sent back to browser. -> HttpContext.Current.Response:包含发送回浏览器的cookie。

Add your cookies to the response instead of the request. 将您的cookie添加到响应而不是请求中。

HttpCookie cookie = HttpContext.Current.Response.Cookies["someCookie"]
if (cookie != null)
  HttpContext.Current.Response.Cookies.Remove(cookie);

HttpCookie updatedCookier = new HttpCookie....
HttpContext.Current.Response.Cookies.Add(updatedCookie);

I am not sure if this will work for you but a few months back i was doing something similar where i manually paste cookies in the HttpWebrequest and it worked so try this: 我不确定这是否适合您,但是几个月前,我在手动将Cookie粘贴到HttpWebrequest中时做了类似的操作,所以请尝试以下操作:

Assuming postReq= WebRequest. 假设postReq = WebRequest。

HttpWebRequest request =(HttpWebRequest)HttpWebRequest.Create("SomeUrl");
postReq.Headers.Add("Cookie: PREF=ID=17337a16083851b1:");// You can get this from fiddler

Post it all in one line.... 全部张贴在一行中...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM