81,091
社区成员
发帖
与我相关
我的任务
分享
public class HTTPSClientTest {
public static void main(String[] args) throws Exception {
HttpClient httpClient = null;
httpClient = new HTTPSTrustClient().init();
// httpClient = new HTTPSCertifiedClient().init();
String url = "https://id.xmu.edu.my/api-auth/login/";
// String url = "https://id.xmu.edu.my/api/malayteachers/";
// String url = "https://1.2.6.2:8011/xxx/api/getHealth";
Map<String, String> paramHeader = new HashMap<>();
paramHeader.put("Accept", "application/json");
Map<String, String> paramBody = new HashMap<>();
//paramBody.put("referrer", "https://id.xmu.edu.my/");
paramBody.put("csrfmiddlewaretoken", "kMTIfPRf7QdGFJ4km2NHmbnUdxM0Ta6TP2JJ6fa8hl1GcmyuJmJdI4dW55ymy5nV");
paramBody.put("next", "/api/malayteachers/");
paramBody.put("id_username", "xxxxxx");
paramBody.put("id_password", "xxxxxx");
String result = HTTPSClientUtil.doPost(httpClient, url, paramHeader, paramBody);
// String result = HTTPSClientUtil.doGet(httpsClient, url, null, null);
System.out.println(result);
}
}
private static void doPost() throws Exception {
HttpClient httpClient = null;
httpClient = new HTTPSTrustClient().init();
// httpClient = new HTTPSCertifiedClient().init();
String url = "https://xxxxxx/";
String username = "";
String password = "";
// String url = "https://1.2.6.2:8011/xxx/api/getHealth";
Map<String, String> paramHeader = new HashMap<>();
paramHeader.put("Accept", "application/json");
String authHeader = new sun.misc.BASE64Encoder().encode((username + ":" + password).getBytes());
//System.out.println(authHeader);
paramHeader.put("Authorization", "Basic " + authHeader);
Map<String, String> paramBody = new HashMap<>();
//paramBody.put("referrer", "https://id.xmu.edu.my/");
//paramBody.put("csrfmiddlewaretoken", "QMBi9iuxCtI0GAREyUvZpVGD3BJdQnYk8U9VdooBwjyofJLksFay6XoEwkdW0nUt");
//paramBody.put("next", "/api/malayteachers/");
/*paramBody.put("username", username);
paramBody.put("password", password);*/
String result = HTTPSClientUtil.doGet(httpClient, url, paramHeader, paramBody);
// String result = HTTPSClientUtil.doGet(httpsClient, url, null, null);
System.out.println(result);
}
ublic class LoginTest extends HttpClientBuilder {
public static void main(String[] args) throws Exception {
// 跳过证书验证
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
// 设置成已信任的证书
ctx.init(null, new TrustManager[] { tm }, null);
ConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(ctx);
// 设置协议http和https对应的处理socket链接工厂的对象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", connectionSocketFactory).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
CookieStore cookieStore = new BasicCookieStore();
HttpClients.custom().setDefaultCookieStore(cookieStore);
// 全局请求设置
RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
// 创建HttpClient上下文
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
// 创建一个HttpClient
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connManager).setDefaultRequestConfig(globalConfig).setDefaultCookieStore(cookieStore).build();
CloseableHttpResponse res = null;
// 创建本地的HTTP内容
try {
try {
// 创建一个get请求用来获取必要的Cookie,如_xsrf信息
HttpGet get = new HttpGet("https://id.xmu.edu.my/api-auth/login");
res = httpClient.execute(get, context);
// 获取常用Cookie,包括_xsrf信息
System.out.println("访问登陆页的获取的常规Cookie:===============");
String token = null;
for (Cookie c : cookieStore.getCookies()) {
System.out.println(c.getName() + ": " + c.getValue());
if ("csrftoken".endsWith(c.getName())) {
token = c.getValue();
}
}
res.close();
// 构造post数据
List<NameValuePair> valuePairs = new LinkedList<NameValuePair>();
valuePairs.add(new BasicNameValuePair("csrfmiddlewaretoken", token));
valuePairs.add(new BasicNameValuePair("next", "/api/malayteachers/"));
valuePairs.add(new BasicNameValuePair("id_username", "id_username"));
valuePairs.add(new BasicNameValuePair("id_password", "id_password"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(valuePairs, Consts.UTF_8);
entity.setContentType("application/x-www-form-urlencoded");
// 创建一个post请求
HttpPost post = new HttpPost("https://id.xmu.edu.my/api-auth/login/?next=/api/malayteachers/");
// 注入post数据
post.setEntity(entity);
res = httpClient.execute(post, context);
res.close();
System.out.println("登陆成功后,新的Cookie:===============");
for (Cookie c : context.getCookieStore().getCookies()) {
System.out.println(c.getName() + ": " + c.getValue());
}
// 构造一个新的get请求,用来测试登录是否成功
HttpGet newGet = new HttpGet("https://id.xmu.edu.my/api/malayteachers/");
res = httpClient.execute(newGet, context);
String content = EntityUtils.toString(res.getEntity());
System.out.println("登陆成功后访问的页面===============");
System.out.println(content);
res.close();
} finally {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}