如何从cookieContainer HttpWebrequest将网站加载到Webrowser

时间:2015-01-14 07:31:31

标签: c# cookies webbrowser-control httprequest cookiecontainer

我尝试通过Httprequest登录网站并回复一些信息。我保存了Cookie响应。那么我怎么知道我登录成功,我尝试用Cookie加载带有新表格的网站,但它没有登录。你能帮帮我吗? 这是我的代码:

登录代码:

HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(new Uri("https://www.nike.com/profile/login?Content-Locale=en_US"));
            getRequest.CookieContainer = new CookieContainer();
            getRequest.CookieContainer.Add(myLoginCookies);
            getRequest.Method = WebRequestMethods.Http.Post;
            getRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            getRequest.UserAgent = userAgent;
            getRequest.Referer = "http://www.nike.com/";
            getRequest.Accept = "application/json, text/javascript, */*; q=0.01";
            getRequest.KeepAlive = true;

            string postData = "login=" + username + "&rememberMe=false&password=" + password;

            try
            {
                using (StreamWriter writer = new StreamWriter(getRequest.GetRequestStream()))
                    writer.Write(postData.ToString());
               HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();

               using (StreamReader reader = new StreamReader(getResponse.GetResponseStream()))
                    returnData = reader.ReadToEnd();
                //MessageBox.Show(returnData);
               myLoginCookies.Add(getResponse.Cookies);
                if (myLoginCookies.Count > 1)
                {
                    return new NikeStore(myLoginCookies, returnData);                
                }
                else
                {
                    MessageBox.Show("login failed");
                    return null;
                }
            }
            catch (WebException ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }

此代码由Cookie返回

加载网站
public Form_OpenCart(CookieCollection oCookie, string url_page)
        {
            InitializeComponent();

            foreach (Cookie cook in oCookie)
            {
                cookie.Add(cook);
            }
            url = url_page;
        }

        private void Form_OpenCart_Load(object sender, EventArgs e)
        {
            url = "http://store.nike.com/us/en_us/";
            if (cookie != null)
            {
                foreach (Cookie cook in cookie)
                {
                    cookie_string += cook.ToString() + ";";
                    InternetSetCookie(url, cook.Name, cook.Value);
                }

                webBrowser1.Navigate(url);               
            }
            else
            {
                MessageBox.Show("huhuhu");
            }

Plz帮助我。我尝试了很多,但我不能成功。

1 个答案:

答案 0 :(得分:0)

在使用HttpWebRequest作为HTTPS网址时,我遇到过很多次问题。所以我切换到Curl Lib c#。它每次都有效。

让我知道您需要示例代码。很可能缺少使用HttpWebRequest的cookie,其中一些是进行会话所需的,所以尝试使用CurlLib并使用它们。

使用此:https://www.dropbox.com/s/0l2rkczij11m0t4/InfoSharing.rar?dl=0

将所有Dll添加到Debug文件夹中,并在项目中仅引用此LibCurlNet.dll 并使用我发送的课程。只需使用它来获取HTML。如果您想使用HttpWebRequest (Get).

,请将帖子显示为空
                    HTTP http = new HTTP();
                    http.setCookie("Cookies\\" + UserName + ".txt");
                    string Url = "";
                    string Post = "";
                    string r = "";

                    //Get info
                    Url = "https://www.example.com/reg/join?trk=hb_join";

                    r = http.Post(Url, Post, "","");