BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #40444同步于 2015/5/4
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

[问题]如何用java的httpclient实现自动登陆授权

kenan1234
2015/5/4镜像同步10 回复
授权页面:https://api.weibo.com/oauth2/authorize?client_id=2454412720&redirect_uri=http://www.baidu.com 再此页面输入新浪用户名和密码,crawltest4@sina.cn ,kaifatest 会回调到百度页面,url为https://www.baidu.com/?code=4d2554be5304ca5fc77920e17a3406bf 后面的code就是我想获得的授权码, 现在用java的httpclient模拟这个登陆过程,可是怎么页面都跳转不了,一值返回状态码200 求助
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
kenan1234机器人#1 · 2015/5/4
代码如下:以前都用得好好的,4月20几号的时候突然就不行了 public static String refreshToken(String accountName, String password) throws IOException { Properties props = new Properties(); try { props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream( "config.properties")); } catch (IOException e) { log.error("Failed to get xinlang token due to " + e.getMessage()); return null; } String url = props.getProperty("authorizeURL"); PostMethod postMethod = new PostMethod(url); postMethod.addParameter("client_id", props.getProperty("client_ID")); postMethod.addParameter("redirect_uri", props.getProperty("redirect_URI")); postMethod.addParameter("userId", accountName); postMethod.addParameter("passwd", password); postMethod.addParameter("isLoginSina", "0"); postMethod.addParameter("action", "submit"); postMethod.addParameter("response_type", "code"); HttpMethodParams param = postMethod.getParams(); param.setContentCharset("UTF-8"); List<Header> headers = new ArrayList<Header>(); headers.add(new Header( "Referer", "https://api.weibo.com/oauth2/authorize?client_id=2454412720&redirect_uri=http://www.baidu.com&from=sina&response_type=code"));// 伪造referer headers.add(new Header("Host", "api.weibo.com")); headers.add(new Header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0")); Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443); Protocol.registerProtocol("https", myhttps); HttpClient client = new HttpClient(); client.getHostConfiguration().getParams().setParameter("http.default-headers", headers); try { client.executeMethod(postMethod); } catch (HttpException e) { log.error("Failed to get xinlang token due to " + e.getMessage()); return null; } catch (IOException e) { log.error("Failed to get xinlang token due to " + e.getMessage()); return null; } int status = postMethod.getStatusCode(); System.out.println(postMethod.getResponseBodyAsString()); if (status != 302) { log.error("Failed to get xinlang token"); return null; } Header location = postMethod.getResponseHeader("Location"); if (location != null) { String retUrl = location.getValue(); int begin = retUrl.indexOf("code="); if (begin != -1) { int end = retUrl.indexOf("&", begin); if (end == -1) end = retUrl.length(); String code = retUrl.substring(begin + 5, end); if (code != null) { Oauth oauth = new Oauth(); AccessToken token; try { token = oauth.getAccessTokenByCode(code); } catch (WeiboException e) { log.error("Failed to get xinlang token due to " + e.getMessage()); return null; } String accesstoken = token.getAccessToken(); log.info("Succeed in getting xinlang token " + accesstoken); return accesstoken; } } } return null; }
agapple机器人#2 · 2015/5/4
bd 通过『我邮2.0』发布
TonyFromDire机器人#3 · 2015/5/5
BD,坐等大神
huayimeng1机器人#4 · 2015/5/5
bd,求大神回答
NotGoodGuy机器人#5 · 2015/5/5
我猜。。。 要不你看看网站登录的post的参数有没有什么变化?或者重定向一下? 要不是你的爬虫被网站封了?换个IP试试?
kenan1234机器人#6 · 2015/5/5
【 在 NotGoodGuy 的大作中提到: 】 : 我猜。。。 : 要不你看看网站登录的post的参数有没有什么变化?或者重定向一下? : 要不是你的爬虫被网站封了?换个IP试试? 感觉不是ip被封了,因为我本机网页操作能得到code,看了一下post的参数感觉就密码那栏为空比较奇怪,
NotGoodGuy机器人#7 · 2015/5/5
之前有吗? 有没有密码被加密传输的可能? 【 在 kenan1234 的大作中提到: 】 : : 感觉不是ip被封了,因为我本机网页操作能得到code,看了一下post的参数感觉就密码那栏为空比较奇怪, : [upload=1][/upload]
zhbzhbzhbz机器人#8 · 2015/5/5
考虑cookie,然后post参数有没有改,还有考虑http头,比如referer或者user-agent啥的,尽量把chrome的f12里的network里的包都看全了。
kuangfengwin机器人#9 · 2015/5/5
围观大神解答