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();
}
}
}