我是编程(C#)的新手,我开始为 MMORPG 编写一个机器人来学习以编程方式与网站通信等等......
我在这里和那里发现了很多关于这个话题的问题。最后在帮助的帮助下,我可以找到一些东西并完成它,不幸的是它不起作用:(
方法:
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;
}
主要的:
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);
似乎登录页面正在做一些事情使它变得更复杂!http://s2.kingsera.org
每一条评论/建议都将不胜感激。提前致谢。