Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpWebRequest Cookies在WinXP上不工作_C#_Cookies_Httpwebrequest_Webrequest_Httpwebresponse - Fatal编程技术网

C# HttpWebRequest Cookies在WinXP上不工作

C# HttpWebRequest Cookies在WinXP上不工作,c#,cookies,httpwebrequest,webrequest,httpwebresponse,C#,Cookies,Httpwebrequest,Webrequest,Httpwebresponse,我在WinXP上运行此程序时遇到问题(在windows 8和windows 7上运行时没有问题),看不出问题所在 我应该在管理页面上登录,但仍然要求我输入密码。(它会看到用户名)。所以我猜是饼干的问题 我使用.NETFramework4 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.We

我在WinXP上运行此程序时遇到问题(在windows 8和windows 7上运行时没有问题),看不出问题所在

我应该在管理页面上登录,但仍然要求我输入密码。(它会看到用户名)。所以我猜是饼干的问题

我使用.NETFramework4

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;
        }

    }

}

我认为在Xp的默认浏览器中没有启用Cookie

要检查是否启用cookie,请在浏览器url中粘贴以下代码,然后按enter键

javascript:alert(navigator.cookieEnabled);

它是什么XP版本(SP,64/32)?WINDOWS XP SP3 32位脚本返回true。但我真的不认为浏览器是个问题,因为我可以从那里毫无问题地登录。