私はプログラミング(C#)が初めてで、MMORPGのボットを書き始めて、プログラムでWebサイトと通信する方法などを学びました...
あちこちで、このトピックに関する多くの質問を見つけました。最後に助けを借りて、何かを見つけて完成させることができましたが、残念ながら機能していません:(
方法:
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
すべてのコメント/提案をいただければ幸いです。前もって感謝します。