I’m new to programming (C#) and I started writing a bot for a MMORPG to learn communicating with websites programmatically and stuff…
I found many questions about this topic in here and there. At last with a helping hand,I could find something and complete it, which unfortunately ain’t working 🙁
The mothods:
public static CookieCollection GetCookie(HttpWebRequest request)
{
if (request.CookieContainer == null)
{ return new CookieContainer().GetCookies(request.RequestUri); }
else
{ return request.CookieContainer.GetCookies(request.RequestUri); }
}
public static CookieContainer GetCookie(HttpWebResponse response)
{
CookieContainer cookiecontainer = new CookieContainer();
cookiecontainer.Add(response.Cookies);
return cookiecontainer;
}
public static void SetCookie(HttpWebRequest request, CookieContainer cookie)
{
request.CookieContainer = cookie;
}
public static void SetCookie(HttpWebResponse response, CookieCollection cookie)
{
response.Cookies = cookie;
}
public static HttpWebResponse PostData(string uri,string request,CookieContainer cookie)
{
HttpWebRequest httprequest;
byte[] requestbytes;
Stream requeststream;
HttpWebResponse httpresponse;
httprequest = (HttpWebRequest)HttpWebRequest.Create(uri);
if (cookie == null)
{httprequest.CookieContainer=new CookieContainer();}
else
{httprequest.CookieContainer=cookie;}
httprequest.Method = "POST";
httprequest.ContentType = "application/x-www-form-urlencoded";
httprequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19";
requestbytes = ASCIIEncoding.ASCII.GetBytes(request);
httprequest.ContentLength = requestbytes.Length;
requeststream=httprequest.GetRequestStream();
requeststream.Write(requestbytes,0,requestbytes.Length);
requeststream.Close();
httpresponse=(HttpWebResponse)httprequest.GetResponse();
if (!(httpresponse.Cookies.Count>0))
{ SetCookie(httpresponse, GetCookie(httprequest)); }
return httpresponse;
}
public static HttpWebResponse GetData(string uri, CookieContainer cookie)
{
HttpWebRequest httprequest;
HttpWebResponse httpresponse;
httprequest = (HttpWebRequest)HttpWebRequest.Create(uri);
if (cookie == null)
{ httprequest.CookieContainer = new CookieContainer(); }
else
{ httprequest.CookieContainer = cookie; }
httprequest.Method = "GET";
httprequest.ContentType = "application/x-www-form-urlencoded";
httprequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19";
httpresponse = (HttpWebResponse)httprequest.GetResponse();
if (!(httpresponse.Cookies.Count > 0))
{ SetCookie(httpresponse, GetCookie(httprequest)); }
return httpresponse;
}
the Main:
string uri = "http://s2.kingsera.org";
string userName = "someUserName";
string passWord = "somePassWord";
string postData = "signinUsername=" + userName + "&signinPassword=" + passWord + "&signinRemember=remember";
CookieContainer cookie = new CookieContainer();
HttpWebResponse response = PostData(uri, postData, cookie);
CookieContainer c = GetCookie(response);
It seems the login page is doing something to make it more complicated! http://s2.kingsera.org
Every single comment/suggestion will be appreciated.
Thanks in advance.
I’m not going to answer your question technically, but when I run into anything like this, I turn to Fiddler, which intercepts web traffic between your computer and a remote server.
That will allow you to see the exact request that your browser is making to the remote server with all it’s headers, cookie data and so on, and the request your code is making to the remote server.
The differences between the two requests should show you the way to solve your problem, or at the very least give you a lot of extra information for troubleshooting.