HttpClient

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();
			}
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值