2

我是编程(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

每一条评论/建议都将不胜感激。提前致谢。

4

1 回答 1

0

我不会从技术上回答你的问题,但是当我遇到这样的事情时,我会求助于Fiddler,它会拦截你的计算机和远程服务器之间的网络流量。

这将允许您查看浏览器向远程服务器发出的确切请求及其所有标头、cookie 数据等,以及您的代码向远程服务器发出的请求。

这两个请求之间的差异应该向您展示解决问题的方法,或者至少为您提供许多额外的故障排除信息。

于 2012-07-25T13:49:23.503 回答