I'm trying to connect to another user page after I logged in using HttpWebRequest
. But when I try it, it connects as not logged in. Does it have to do with cookies?
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();
Here this code solves the problem, you should add an CookieContainer to your request to handle the session. The site returned a html containning "Thank you for logging in, eferek", and now the sessionID is stored in our CookieContainer, so every time you create a new request use the same CookieContainer and you will have the same session at the site.
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;
}
}
}