日期:2014-05-20  浏览次数:20700 次

关于httpClient实现模拟登录的问题。
怎么知道你要登录的网站地址呢?(比如51job.com,模拟登录时的地址又是什么呢?求方法!)


HttpGet可以实现模拟登录吗?参数怎么带过去?(求明示!)
HttpPost提交时可以配置其List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 但是这个里边又add什么东西呢?(求详细明示!)有些网站String charset = EntityUtils.getContentCharSet(entity); 拿到的编码格式是null,不转码的话就会是乱码。。。又一次伤了!










还有最重要的一点哦,
有些网站登录成功之后会转发,不是这种Header header = response.getFirstHeader("Location");

而是如果页面配置了一个定时器,比如5s之后去windows.location.replace("登录之后的主页地址");
这种又要怎么搞呢, 我疯了!

------解决方案--------------------
楼主您好 httpclient可以完成你想做的事情 下面是鄙人的代码供参考,经证实,登陆成功,且可以看到“我的简历”成功登陆标识
Java code

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class Login51Job {
        private HttpClient client;  
        private PostMethod post;  
        private static String name = "my name";//你懂得
        private static String password = "my pwd";//^_^
        private static String pageUrl = "http://my.51job.com/my/My_Pmc.php";//地址
          
        private NameValuePair ie = new NameValuePair("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.50727)");  
        private NameValuePair url = new NameValuePair("url","%2Fmy%2FMy_Pmc.php");  
        private NameValuePair x = new NameValuePair("x", "67");  
        private NameValuePair y = new NameValuePair("y", "18"); 
        private NameValuePair nameVP =  new NameValuePair("username", name);
        private NameValuePair pwdVP = new NameValuePair("userpwd", password);
        public Login51Job() {  
            client = new HttpClient();  
            client.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
            post = new PostMethod(pageUrl);  
        }  
          
        public void execute() throws HttpException, IOException {  
            NameValuePair[] p = new NameValuePair[]{ie,url,x,y,nameVP,pwdVP};  
            post.setRequestBody(p);  
            client.executeMethod(post);  
            //保存登陆成功后的cookie
            Cookie[] cookies = client.getState().getCookies();  
            client.getState().addCookies(cookies);  
            post.releaseConnection(); 
            
            GetMethod get = new GetMethod(pageUrl);  
            client.executeMethod(get);  
            String homePage = new String(get.getResponseBodyAsString());
            String regex = null;
            Pattern pattern = null;
            Matcher matcher = null;
            regex = "<li><a href=\"([^<]*?)\">我的简历</a>";
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(homePage);
            String resumeNo="";
            if(matcher.find()){
                resumeNo = matcher.group(1);
                System.out.println("获取简历需要的编号url:"+resumeNo);
            }
            get.releaseConnection();
            String resumeListUrl = resumeNo;
            if(resumeListUrl != ""){
                System.out.print("简历地址是:"+resumeListUrl+" 登陆成功");
            }else{
                System.out.print("登陆失败");
            }
        }  
          
        public static void main(String[] args) throws HttpException, IOException {  
            Login51Job job = new Login51Job();  
            job.execute();  
        }  
}

------解决方案--------------------