これを 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;
}
}
}