java httpclient登录_java commons HttpClient登录oschina网站代码

package com.main;

import java.io.InputStreamReader;

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

import org.apache.commons.httpclient.Cookie;

import org.apache.commons.httpclient.HttpClient;

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 Login {

public static void main(String[] args) {

String loginUrl = "https://www.oschina.net/action/user/hash_login";

String email = "email";

String password = "password";

try {

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = manager.getEngineByName("JavaScript");

engine.eval(new InputStreamReader(Login.class

.getResourceAsStream("/sha1.js")));

Object t = engine.eval("CryptoJS.SHA1('" + password

+ "').toString();");

System.out.println(t);

HttpClient client = new HttpClient();

NameValuePair[] nameValuePairs = {

new NameValuePair("email", email),

new NameValuePair("pwd", t.toString()) };

PostMethod postMethod = new PostMethod(loginUrl);

postMethod.setRequestBody(nameValuePairs);

client.getParams().setCookiePolicy(

CookiePolicy.BROWSER_COMPATIBILITY);

client.executeMethod(postMethod);

Cookie[] cookies = client.getState().getCookies();

String tmpcookies = "";

for (Cookie c : cookies) {

tmpcookies += c.toString() + ";";

}

GetMethod getMethod = new GetMethod(

"http://my.oschina.net/snageyang/admin/inbox");

getMethod.setRequestHeader("cookie", tmpcookies);

client.executeMethod(getMethod);

String text = getMethod.getResponseBodyAsString();

System.out.println(text);

} catch (Exception e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值