-1

使用 . 登录后,我正在尝试连接到另一个用户页面HttpWebRequest。但是当我尝试它时,它以未登录的方式连接。它与cookie有关吗?

req = (HttpWebRequest)HttpWebRequest.Create("http://www.elitepvpers.com/forum/login.php?do=login");
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1";
req.Method = "POST";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers.Add("Accept-Language: tr-tr,tr;q=0.8,en-us;q=0.5,en;q=0.3");
req.KeepAlive = true;
req.Referer = "Referer: http://www.elitepvpers.com/theblackmarket/treasures/";
req.ContentType = "application/x-www-form-urlencoded";
string id = "username";
string pw = "*****";
StreamWriter sw = new StreamWriter(req.GetRequestStream());
sw.Write("vb_login_username=" + id + "&vb_login_password=" + pw + "&cookieuser=1&s=&securitytoken=1342962102-8aa76183509ebade0188ac49c3c84470ff2aabba&do=login&vb_login_md5password=&vb_login_md5password_utf=");
response = (HttpWebResponse)req.GetResponse();
4

1 回答 1

1

这段代码解决了这个问题,你应该在你的请求中添加一个 CookieContainer 来处理会话。该站点返回了一个包含“感谢您登录,eferek”的html,现在sessionID 存储在我们的CookieContainer 中,因此每次创建新请求时都使用相同的CookieContainer,您将在站点上拥有相同的会话。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace del2
{
    class Program
    {
        static void Main(string[] args)
        {

            HttpWebRequest request = GetNewRequest("http://www.elitepvpers.com/forum/login.php?do=login", cookies);


            string id = "efeerk";
            string pw = "trgamer";
            Dictionary<string,string> parameters = new Dictionary<string,string>{{"vb_login_username",id},{"vb_login_password",pw},
                {"cookieuser","1"},{"s",""},{"securitytoken","1342962102-8aa76183509ebade0188ac49c3c84470ff2aabba"},
                {"do","login"},{"vb_login_md5password",""},{"vb_login_md5password_utf",""}};


            HttpWebResponse response = MakeRequest(request, cookies, parameters);
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                if(!reader.EndOfStream)
                {
                    Console.Write(reader.ReadToEnd());
                }
            }

            Console.Read();

        }

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



    }
}
于 2012-07-22T16:13:47.147 回答