[英]c# web browser cookies in httpwebrequest
I have a webbrowser control which loads a page. 我有一个加载页面的webbrowser控件。 I then hit a button to call this method: 然后,我按下按钮以调用此方法:
public void get(Uri myUri)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
CookieContainer cookieJar = new CookieContainer();
cookieJar.SetCookies(webBrowser1.Document.Url,webBrowser1.Document.Cookie.Replace(';', ','));
request.CookieContainer = cookieJar;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
int cookieCount = cookieJar.Count;
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
txt.Text = readStream.ReadToEnd();
txt2.Text = cookieCount.ToString();
}
As from the cookieCount int i can see that if i call the method before logging in on the page in the web browser control i would get 6 cookies, and after i log in i get 7. However, even with the cookies the response i get is the same as if i wouldnt have been logged in. So i am guessing that the cookies isnt being sent with the request? 从cookieCount int可以看出,如果我在登录Web浏览器控件中的页面之前调用该方法,我将获得6个cookie,而登录后获得7个cookie。但是,即使使用cookie,我也会得到响应就像我不会登录一样。因此,我猜想cookie是否不随请求一起发送?
Thanks! 谢谢!
When matching the session, your web server may be taking into account some other HTTP request headers , besides cookies. 匹配会话时,您的Web服务器可能会考虑cookie之外的其他一些HTTP请求标头 。 To name a few: User-Agent
, Authorization
, Accept-Language
. 仅举几例: User-Agent
, Authorization
, Accept-Language
。
Because WebBrowser
control and WebRequest
do not share sessions, you'd need to replicate all headers from the WebBrowser
session. 由于WebBrowser
控件和WebRequest
不共享会话,因此您需要复制WebBrowser
会话中的所有标头。 This would be hard thing to do, as you'd need to intercept WebBrowser
trafic, in a way similar to what Fiddler does. 这将是一件很难的事,因为您需要以类似于Fiddler的方式来拦截WebBrowser
流量。
A more feasible solution might be to stay on the same session with WebBrowser
by using Windows UrlMon APIs like URLOpenStream
, URLDownloadToFile
etc., instead of WebRequest
. 一个更可行的解决方案可能是通过使用Windows UrlMon API(例如URLOpenStream
, URLDownloadToFile
等)而不是WebRequest
与WebBrowser
保持相同的会话。 That works, because WebBrowser
uses UrlMon
library behind the scene. 之所以UrlMon
,是因为WebBrowser
在后台使用了UrlMon
库。
I've recently answered some related questions: 我最近回答了一些相关问题:
You're recreating your CookieContainer every time you call this method, you need to use the same CookieContainer in all requests 每次调用此方法时,您都要重新创建CookieContainer,您需要在所有请求中使用相同的CookieContainer
you can use this code, to handle your requests: 您可以使用以下代码来处理您的请求:
static CookieContainer cookies = new CookieContainer();
static HttpWebRequest GetNewRequest(string targetUrl, CookieContainer SessionCookieContainer)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);
request.CookieContainer = SessionCookieContainer;
request.AllowAutoRedirect = false;
return request;
}
public static HttpWebResponse MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary<string, string> parameters = null)
{
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Accept: */*";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.CookieContainer = SessionCookieContainer;
request.AllowAutoRedirect = false;
if (parameters != null)
{
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string postData = "";
foreach (KeyValuePair<String, String> parametro in parameters)
{
if (postData.Length == 0)
{
postData += String.Format("{0}={1}", parametro.Key, parametro.Value);
}
else
{
postData += String.Format("&{0}={1}", parametro.Key, parametro.Value);
}
}
byte[] postBuffer = UTF8Encoding.UTF8.GetBytes(postData);
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(postBuffer, 0, postBuffer.Length);
}
}
else
{
request.Method = "GET";
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
SessionCookieContainer.Add(response.Cookies);
while (response.StatusCode == HttpStatusCode.Found)
{
response.Close();
request = GetNewRequest(response.Headers["Location"], SessionCookieContainer);
response = (HttpWebResponse)request.GetResponse();
SessionCookieContainer.Add(response.Cookies);
}
return response;
}
and to request a page, 并请求一个页面,
HttpWebRequest request = GetNewRequest("http://www.elitepvpers.com/forum/login.php?do=login", cookies);
Dictionary<string,string> parameters = new Dictionary<string,string>{{"your params","as key value"};
HttpWebResponse response = MakeRequest(request, cookies, parameters);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
if(!reader.EndOfStream)
{
Console.Write(reader.ReadToEnd());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.