HttpWebRequest Cookie 在 WinXP 上不起作用
本文关键字:不起作用 WinXP Cookie HttpWebRequest | 更新日期: 2023-09-27 18:30:40
我在WinXP上运行它时遇到问题(在Windows 8和Windows 7上运行没有问题),并且看不到问题所在。
我应该登录管理页面,但仍然要求我输入密码。(它看到用户名)。所以我假设是饼干的东西。
我使用 .net 框架 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class dataDumper
{
CookieCollection cookie_Collection = new CookieCollection();
CookieContainer cookie_Container = new CookieContainer();
static string username = "MY_Username";
static string password = "MY_Password";
static string domain = "https://www.infojobs.it";
string linklogin = domain + "/employer-login-run.xhtml";
string linkmanagement = domain + "/employer/manage/offers/management-view.xhtml";
string postData = String.Format("email=" + username + "&password=" + password + "&j_clientId=empleo_it&submit=Entra");
public void Start()
{
webPageHome();
webPageLogin();
webPageManagement();
}
#region PageDumper
private void webPageHome()
{
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(domain);
getRequest.CookieContainer = cookie_Container;
getRequest.CookieContainer.Add(cookie_Collection);
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
cookie_Collection = getResponse.Cookies;
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream(), Encoding.Default, true))
{
//read the login page
string loginsourceCode = sr.ReadToEnd();
File.WriteAllText("home.txt", System.Web.HttpUtility.HtmlDecode(loginsourceCode));
}
}
private void webPageLogin()
{
HttpWebRequest getRequest = webRequest(linklogin, postData);
getRequest.CookieContainer.Add(cookie_Collection);
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
cookie_Collection = getResponse.Cookies;
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream(), Encoding.Default, true))
{
//read the login page
string loginsourceCode = sr.ReadToEnd();
File.WriteAllText("login.txt", System.Web.HttpUtility.HtmlDecode(loginsourceCode));
}
}
private void webPageManagement()
{
HttpWebRequest getRequest = webRequest(linkmanagement, postData);
getRequest.CookieContainer.Add(cookie_Collection);
HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
cookie_Collection = getResponse.Cookies;
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream(), Encoding.Default, true))
{
//read the management page
string managementsourceCode = sr.ReadToEnd();
//parse the result
File.WriteAllText("management.txt", System.Web.HttpUtility.HtmlDecode(managementsourceCode));
}
}
#endregion
private HttpWebRequest webRequest(string getUrl, string postData = null)
{
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = cookie_Container;
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";
if (postData != null)
{
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream(); //open connection
newStream.Write(byteArray, 0, byteArray.Length); // Send the data.
newStream.Close();
}
else
{
getRequest.GetRequestStream();
}
return getRequest;
}
}
}
我认为 Cookie 在 Xp 上的默认浏览器中未启用
以检查是否启用cookie,请将以下代码粘贴到您的浏览器URL中,然后按回车键。
javascript:alert(navigator.cookieEnabled);