GET请求
public static void test01() {
CloseableHttpClient client = HttpClients.createDefault();
String url = "http://httpbin.org/get";
HttpGet get = new HttpGet(url);
get.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36");
get.addHeader("Referer","https://blog.csdn.net/lc352778616/article/details/54314070");
CloseableHttpResponse response = null;
try {
response = client.execute(get);
org.apache.http.HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,"utf-8");
System.out.println("==========> "+result);
EntityUtils.consume(entity);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(null != client) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
GET带参
public static void main(String[] args) {
CloseableHttpClient client = HttpClients.createDefault();
URIBuilder uriBuilder;
HttpGet get = null;
try {
uriBuilder = new URIBuilder("http://httpbin.org/get");
uriBuilder.setParameter("keys","Java");
get = new HttpGet(uriBuilder.build());
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
CloseableHttpResponse response = null;
try {
response = client.execute(get);
org.apache.http.HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,Consts.UTF_8);
System.out.println("==========> "+result);
EntityUtils.consume(entity);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(null != client) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
POST请求
public static void test02() {
CloseableHttpClient client = HttpClients.createDefault();
String url = "http://httpbin.org/post";
HttpPost post = new HttpPost(url);
post.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36");
post.addHeader("Referer","https://blog.csdn.net/lc352778616/article/details/54314070");
List<NameValuePair> list = Lists.newArrayList();
list.add(new BasicNameValuePair("a", "1"));
list.add(new BasicNameValuePair("b", "2"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list,Consts.UTF_8);
post.setEntity(formEntity);
CloseableHttpResponse response = null;
try {
response = client.execute(post);
org.apache.http.HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,"utf-8");
System.out.println("==========> "+result);
EntityUtils.consume(entity);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(null != client) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
POST发送JSON
public static void test03(String[] args) {
CloseableHttpClient client = HttpClients.createDefault();
String url = "http://httpbin.org/post";
HttpPost post = new HttpPost(url);
post.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36");
post.addHeader("Referer","https://blog.csdn.net/lc352778616/article/details/54314070");
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "xxxx");
jsonObject.put("password", "aaaa");
StringEntity jsonEntity = new StringEntity(jsonObject.toString(),Consts.UTF_8);
jsonEntity.setContentType(new BasicHeader("Content-Type", "application/json"));
jsonEntity.setContentEncoding(Consts.UTF_8.name());
post.setEntity(jsonEntity);
CloseableHttpResponse response = null;
try {
response = client.execute(post);
org.apache.http.HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,"utf-8");
System.out.println("==========> "+result);
EntityUtils.consume(entity);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(null != client) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
DELETE发送JSON
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";
public String getMethod() { return METHOD_NAME; }
public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpDeleteWithBody() { super(); }
}
使用
public static void main(String[] args) {
String url = "http://httpbin.org/delete";
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpDeleteWithBody delete = new HttpDeleteWithBody(url);
delete.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36");
delete.addHeader("Referer","https://blog.csdn.net/lc352778616/article/details/54314070");
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "xxxx");
jsonObject.put("password", "aaaa");
StringEntity jsonEntity = new StringEntity(jsonObject.toString(),Consts.UTF_8);
jsonEntity.setContentType(new BasicHeader("Content-Type", "application/json"));
jsonEntity.setContentEncoding(Consts.UTF_8.name());
delete.setEntity(jsonEntity);
CloseableHttpResponse response = null;
try {
response = client.execute(delete);
org.apache.http.HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,"utf-8");
System.out.println("==========> "+result);
EntityUtils.consume(entity);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(null != client) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}