java httpclient4.5_Java中的httpclient4.5应该怎么使用?

import java.io.IOException;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

@SuppressWarnings("deprecation")

public class HttpUtil {

private static final String UTF_8 = HTTP.UTF_8;

public static String post(String url , Map params) throws Exception{

DefaultHttpClient client = HttpFactory.createHttpClient();

HttpPost post = new HttpPost(url);

if(params != null ){

List lparams = new LinkedList();

for (ConcurrentHashMap.Entry entry : params.entrySet()) {

if (entry.getValue() != null) {

lparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));

}else{

lparams.add(new BasicNameValuePair(entry.getKey(), ""));

}

}

HttpEntity entiry = new UrlEncodedFormEntity(lparams, UTF_8);

post.setEntity(entiry);

}

try {

HttpResponse resonse = client.execute(post);

return entityToString(resonse);

} catch (Exception exception) {

throw exception;

} finally {

post.abort();

client.getConnectionManager().shutdown();

}

}

public static String get(String url) throws Exception{

DefaultHttpClient client = HttpFactory.createHttpClient();

HttpGet get = new HttpGet(url);

try {

HttpResponse resonse = client.execute(get);

return entityToString(resonse);

} catch (Exception exception) {

throw exception;

} finally {

get.abort();

client.getConnectionManager().shutdown();

}

}

public static String entityToString(HttpResponse resonse) throws Exception{

HttpEntity entity = resonse.getEntity();

if (entity != null) {

String msg = null;

try {

msg = EntityUtils.toString(entity, UTF_8);

} catch (IOException e) {

e.printStackTrace();

}

int code = resonse.getStatusLine().getStatusCode();

if (code == 200) {

return msg;

} else {

String errerMsg = (msg == null ? null : msg);

throw new Exception("http code:" + code +",error:"+ errerMsg);

}

}

throw new Exception("http entity is null");

}

public static byte[] entityTobyte(HttpResponse resonse) throws Exception {

HttpEntity entity = resonse.getEntity();

if (entity != null) {

byte[] buffer = null;

try {

buffer = EntityUtils.toByteArray(entity);

} catch (IOException e) {

e.printStackTrace();

}

int code = resonse.getStatusLine().getStatusCode();

if (code == 200) {

return buffer;

} else {

String errerMsg = (buffer == null ? null : new String(buffer, UTF_8));

throw new Exception("http code:" + code +",error:"+ errerMsg);

}

}

throw new Exception("http entity is null");

}

}

在官方包中是有例子的:

贴几个给看看 模拟表达登录请求

package org.apache.http.examples.client;

import java.net.URI;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpUriRequest;

import org.apache.http.client.methods.RequestBuilder;

import org.apache.http.cookie.Cookie;

import org.apache.http.impl.client.BasicCookieStore;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

/**

* A example that demonstrates how HttpClient APIs can be used to perform

* form-based logon.

*/

public class ClientFormLogin {

public static void main(String[] args) throws Exception {

BasicCookieStore cookieStore = new BasicCookieStore();

CloseableHttpClient httpclient = HttpClients.custom()

.setDefaultCookieStore(cookieStore)

.build();

try {

HttpGet httpget = new HttpGet("https://someportal/");

CloseableHttpResponse response1 = httpclient.execute(httpget);

try {

HttpEntity entity = response1.getEntity();

System.out.println("Login form get: " + response1.getStatusLine());

EntityUtils.consume(entity);

System.out.println("Initial set of cookies:");

List cookies = cookieStore.getCookies();

if (cookies.isEmpty()) {

System.out.println("None");

} else {

for (int i = 0; i < cookies.size(); i++) {

System.out.println("- " + cookies.get(i).toString());

}

}

} finally {

response1.close();

}

HttpUriRequest login = RequestBuilder.post()

.setUri(new URI("https://someportal/"))

.addParameter("IDToken1", "username")

.addParameter("IDToken2", "password")

.build();

CloseableHttpResponse response2 = httpclient.execute(login);

try {

HttpEntity entity = response2.getEntity();

System.out.println("Login form get: " + response2.getStatusLine());

EntityUtils.consume(entity);

System.out.println("Post logon cookies:");

List cookies = cookieStore.getCookies();

if (cookies.isEmpty()) {

System.out.println("None");

} else {

for (int i = 0; i < cookies.size(); i++) {

System.out.println("- " + cookies.get(i).toString());

}

}

} finally {

response2.close();

}

} finally {

httpclient.close();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值