求助httpclient模拟登录百度https的问题原来的用httpclient3.1的,现在用不了,谁能帮忙修改下,或是帮忙提供
求助httpclient模拟登录百度https的问题
原来的用httpclient3.1的,现在用不了,谁能帮忙修改下,
或是帮忙提供httpclient4.0的模拟登录代码或是其他的代码?
谢谢。
- Java code
public int publisharticle(User bu) { HttpClient client=null; HttpMethodParams params=new HttpMethodParams(); boolean pubSuccess=false; try { Protocol authhttps = new Protocol("https", new AuthSSLProtocolSocketFactory( new URL("file:///E:/https/my.keystore"), "1088111", new URL("file:///E:/https/my.truststore"), "aaaaa"), 443); client = new HttpClient(); //中文乱码 String CONTENT_CHARSET = "gb2312"; client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET); client.getHostConfiguration().setHost("passport.baidu.com", 443, authhttps); /* 只能使用相对路径 */ GetMethod httpget = new GetMethod("/?login&tpl=mn"); client.executeMethod(httpget); //System.out.println(new String(httpget.getResponseBody(),"gb2312")); PostMethod httppost=new PostMethod("https://passport.baidu.com/?login"); httppost.setParameter("tpl_ok", ""); httppost.setParameter("next_target", ""); httppost.setParameter("tpl", "mn"); httppost.setParameter("skip_ok", ""); httppost.setParameter("aid", ""); httppost.setParameter("need_pay", ""); httppost.setParameter("need_coin", ""); httppost.setParameter("pay_method", ""); httppost.setParameter("u", "http://www.baidu.com/"); httppost.setParameter("return_method", "get"); httppost.setParameter("more_param", ""); httppost.setParameter("return_type", ""); httppost.setParameter("psp_tt", "0"); httppost.setParameter("password", bu.getPassword()); httppost.setParameter("safeflg", "0"); httppost.setParameter("isphone", "tpl"); httppost.setParameter("username", bu.getUsername()); httppost.setParameter("verifycode", ""); httppost.setParameter("mem_pass", "on"); client.executeMethod(httppost); StringBuffer sb=new StringBuffer(); Header[] heads=httppost.getResponseHeaders(); for (int i=0;i<heads.length;i++){ // System.out.println(i+":"+heads[i].getName()+":"+heads[i].getValue()); if(heads[i].getName().equalsIgnoreCase("Set-Cookie")){ sb.append(heads[i].getValue()+";"); } } System.out.println("================"+new String(sb)+"======================"); int i=0; InputStream ins=httppost.getResponseBodyAsStream(); while ((i=ins.read())!=-1){ // System.out.print(i); } httppost.abort(); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (HttpException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
[解决办法]
原来能用现在用不了多数是网站变化了,增加了信息校验之类的
报错中能看出来什么吗
[解决办法]
先找到原因吧:用httpclient3.1和httpclient4.0都测试一下。。看看是不是都不能登录进去,然后判断是百度更新了。。还是4.0的问题。。找到问题才好解决吧。。
具体怎么解决。。我表示:只能帮顶。。。。
[解决办法]
楼主你要做什么操作? 就是打开百度,然后登陆百度账号,再回到百度主页是吗?
[解决办法]
httpClient 4.0.X
百度的编码似乎有些恶心。
- Java code
/** * 测试百度登陆 * @author 疯子 * */public class BaiDuLogin { private static DefaultHttpClient httpclient; private Logger log = Logger.getLogger(this.getClass().getName()); public static void main(String[] args) { new BaiDuLogin(); } public BaiDuLogin() { try{ httpclient = new DefaultHttpClient(); httpclient = WebClientDevWrapper.wrapClient(httpclient); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("aid", "")); nvps.add(new BasicNameValuePair("isphone", "tpl")); nvps.add(new BasicNameValuePair("mem_pass", "on")); nvps.add(new BasicNameValuePair("password", "bafqpqtd")); nvps.add(new BasicNameValuePair("return_method", "get")); nvps.add(new BasicNameValuePair("safeflg", "0")); nvps.add(new BasicNameValuePair("tpl", "mn")); nvps.add(new BasicNameValuePair("u", "http://www.baidu.com/")); nvps.add(new BasicNameValuePair("username", "chenfeng432")); HttpPost httpost = new HttpPost("https://passport.baidu.com/?login"); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); if (entity != null) { log.info("登陆返回的html=="+IOUtils.toString(entity.getContent(), "GB2312")); } HttpGet httpget = new HttpGet("http://www.baidu.com"); response = httpclient.execute(httpget); entity = response.getEntity(); if (entity != null) { log.info("登陆过后再次访问百度主页返回的html:"+IOUtils.toString(entity.getContent(), "UTF-8")); } }catch (Exception e) { // TODO: handle exception log.error("出错", e); } }}
[解决办法]
楼主,你这个问题解决了吗? 我也遇到这问题。
