有哪位大哥帮我看下用c#的httpwebrequest模拟登陆www.edeng.cn这个网址,简单的post登陆我会,但是这个网址的登陆方式好像是用session存的值,我用httpwatch工具跟踪过,就是找不出来。麻烦大虾们帮我看下。

解决方案 »

  1.   

    好像没什么难度吧!
    1.以get方式访问 http://www.edeng.cn/code/bin/login/login_form.php?a=1&f=%2Fcode%2Fbin%2Fpost%2Fchoice_cat_geo.php%3Fgeotrace%3D1%26cattrace%3D
    2.设置request的referer =http://www.edeng.cn/code/bin/login/login_form.php?a=1&f=%2Fcode%2Fbin%2Fpost%2Fchoice_cat_geo.php%3Fgeotrace%3D1%26cattrace%3D
    3.将数据:
    f=%2Fcode%2Fbin%2Fpost%2Fchoice_cat_geo.php%3Fgeotrace%3D1%26cattrace%3D&a=1&login_id=%E4%B8%AD%E5%9B%BD%E4%BA%BAXXX{编码后的用户名}&password=19741216{登录密码}&sub1=   post给:http://www.edeng.cn/bin/login/login.php地址(提交后需要将cookie保留住)就这样就可以了
      

  2.   

    我也是这样做的,但是post时候总是返回一个错误"The remote server returned an error: (417) Expectation failed.", 大致代码如下:
    /// <summary>
            /// 获取指定地址的html
            /// </summary>
            /// <param name=\"URL"></param>
            /// <param name=\"PostData"></param>
            /// <param name=\"encoding"></param>
            /// <returns></returns>
            public string GetHTML(string URL, string PostData, System.Text.Encoding encoding)
            {
                try
                {
                    _Err = "OK";
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
                    //image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword,
                    request.Accept = "*/*";
                    request.Referer = this.Referer;
                    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727;.NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
                    request.CookieContainer = this.ReqCookies;
                    request.AllowAutoRedirect = true;
                    request.Timeout = 50000;
                    
                    if (encoding == null)
                        encoding = System.Text.Encoding.UTF8;
                    //提交的数据
                    if (PostData != null && PostData.Length > 0)
                    {
                        request.ContentType = "application/x-www-form-urlencoded";
                        request.Method = "POST";
                        byte[] b = encoding.GetBytes(PostData);
                        request.ContentLength = b.Length;
                        using (System.IO.Stream sw = request.GetRequestStream())
                        {
                            try
                            {
                                sw.Write(b, 0, b.Length);
                            }
                            catch
                            {
                                _Err = "KeepAliveFailure";
                                _Html = "";
                                return _Html;
                            }
                            finally
                            {
                                if (sw != null) { sw.Close(); }
                            }
                        }
                    }
                    HttpWebResponse response = null;
                    System.IO.StreamReader sr = null;
                    try
                    {
                        response = (HttpWebResponse)request.GetResponse();
                        sr = new System.IO.StreamReader(response.GetResponseStream(), encoding);
                        _Html = sr.ReadToEnd();
                    }
                    catch (WebException webex)
                    {
                        if (webex.Status == WebExceptionStatus.KeepAliveFailure)
                        {
                            _Html = "";
                            _Err = "KeepAliveFailure";
                            return _Html;
                        }
                        else
                        {
                            _Html = "";
                            _Err = webex.Message;
                            return _Html;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        _Html = "";
                        _Err = ex.Message;
                        return _Html;
                    }
                    finally
                    {
                        if (sr != null) { sr.Close(); }
                        if (response != null) { response.Close(); }
                        response = null;
                        request = null;
                    }                return _Html;
                }
                catch (WebException we)
                {
                    return "";
                }
            }
    外面就是这样调用的:
                    string htmlLogin = func.GetHTML("http://www.edeng.cn/code/bin/login/login_form.php?a=1&f=%2Fcode%2Fbin%2Fpost%2Fchoice_cat_geo.php%3Fgeotrace%3D1%26cattrace%3D", "", Encoding.UTF8);                func.Referer = "http://www.edeng.cn/code/bin/login/login_form.php?a=1&f=%2Fcode%2Fbin%2Fpost%2Fchoice_cat_geo.php%3Fgeotrace%3D1%26cattrace%3D";                string post = String.Format("f=%2Fcode%2Fbin%2Fpost%2Fchoice_cat_geo.php%3Fgeotrace%3D1%26cattrace%3D&a=1&login_id={0}&password={1}&sub1=", "a111111111", "123456");
                    htmlLogin = func.GetHTML("http://www.edeng.cn/bin/login/login.php", post, Encoding.UTF8);
      

  3.   

    我有用httpwatch抓过,也跟过js脚本,发现js脚本中取cookies的一个userid,我有保存cookies啊,为什么不能成功.
      

  4.   

    可以用webbrowser实现,直接模拟提交过去,不用管cookie什么的