本文内容大多基于官方文档和网上前辈经验总结,经过个人实践加以整理积累,仅供参考。
1.1 web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>loginServiceservlet-name>
<servlet-class>service.LoginServiceservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>loginServiceservlet-name>
<url-pattern>/loginurl-pattern>
servlet-mapping>
web-app>
1.2 Servlet
package service;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginService extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
OutputStream outputStream = response.getOutputStream();
response.setHeader("content-type", "application/json;charset=UTF-8");
String result = null;
if (username == null) {
result = "请输入用户名!";
} else if (password == null) {
result = "请输入密码!";
} else if (!username.equals("unique")) {
result = "用户名错误!";
} else if (!password.equals("ASDF")) {
result = "密码错误!";
} else {
result = "验证通过~~";
}
outputStream.write(result.getBytes("UTF-8"));
}
}
2.1 添加 Apache HttpClient 依赖包
本文使用 Maven 管理依赖,关于 Maven 的使用可以参看:Apache Maven 或上网搜索相关资料
注意:本文使用的 Apache HttpClient 版本是 4.5.3
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>jylgroupId>
<artifactId>ServiceartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.3version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
project>
2.2 编写单元测试
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class TestHttpClient {
@Test
public void test() {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:8080/Service/login");
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
System.out.print("Protocol Version :: ");
System.out.println(httpResponse.getProtocolVersion());
System.out.println("--------------------------------------------------");
System.out.print("Status Code :: ");
System.out.println(httpResponse.getStatusLine().getStatusCode());
System.out.println("--------------------------------------------------");
System.out.print("Reason Phrase :: ");
System.out.println(httpResponse.getStatusLine().getReasonPhrase());
System.out.println("--------------------------------------------------");
System.out.print("Status Line :: ");
System.out.println(httpResponse.getStatusLine().toString());
System.out.println("--------------------------------------------------");
HttpEntity httpEntity = httpResponse.getEntity();
System.out.print("Content Length :: ");
System.out.println(httpEntity.getContentLength());
System.out.println("--------------------------------------------------");
System.out.print("Content Type :: ");
System.out.println(httpEntity.getContentType());
System.out.println("--------------------------------------------------");
System.out.print("Content :: ");
System.out.println(httpEntity.getContent());
System.out.println("--------------------------------------------------");
System.out.print("Content Text :: ");
System.out.println(EntityUtils.toString(httpEntity));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.4 修改 HttpGet 参数测试返回的 Content Text
HttpGet httpGet = new HttpGet("http://localhost:8080/Service/login?username=unique");
HttpGet httpGet = new HttpGet("http://localhost:8080/Service/login?username=unique&password=ASDF");
官方文档中建议严格控制使用 EntityUtils 读取 HttpEntity 的信息,除非请求的 HTTP 服务器是一个授信的服务器且返回的结果长度在一定范围内,下面是官方文档中的原文
However, the use of EntityUtils is strongly discouraged unless the response entities originate from a trusted HTTP server and are known to be of limited length.
可以向 HttpGet 传递一个 java.net.URI 参数代替字符串参数
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class TestHttpClient {
@Test
public void test() {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = null;
CloseableHttpResponse httpResponse = null;
try {
URI uri = new URIBuilder()
.setScheme("http")
.setHost("localhost")
.setPort(8080)
.setPath("/Service/login")
.setParameter("username", "unique")
.setParameter("password", "ASDF")
.build();
httpGet = new HttpGet(uri);
httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
System.out.println(EntityUtils.toString(httpEntity));
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5.1 将 Servlet 的 doGet 方法修改为 doPost,逻辑不变
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
OutputStream outputStream = response.getOutputStream();
response.setHeader("content-type", "application/json;charset=UTF-8");
String result = null;
if (username == null) {
result = "请输入用户名!";
} else if (password == null) {
result = "请输入密码!";
} else if (!username.equals("unique")) {
result = "用户名错误!";
} else if (!password.equals("ASDF")) {
result = "密码错误!";
} else {
result = "验证通过~~";
}
outputStream.write(result.getBytes("UTF-8"));
}
5.2 使用 org.apache.http.client.methods.HttpPost 替换 HttpGet
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class TestHttpClient {
@Test
public void test() {
List params = new ArrayList<>();
params.add(new BasicNameValuePair("username", "unique"));
params.add(new BasicNameValuePair("password", "ASDF"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
HttpPost httpPost = new HttpPost("http://localhost:8080/Service/login");
httpPost.setEntity(entity);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
System.out.println(EntityUtils.toString(httpEntity));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5.3 同样给以给 HttpPost 传递 java.net.URI 参数取代字符串参数
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class TestHttpClient {
@Test
public void test() {
List params = new ArrayList<>();
params.add(new BasicNameValuePair("username", "unique"));
params.add(new BasicNameValuePair("password", "ASDF"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
try {
URI uri = new URIBuilder()
.setScheme("http")
.setHost("localhost")
.setPort(8080)
.setPath("/Service/login")
.build();
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(entity);
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
System.out.println(EntityUtils.toString(httpEntity));
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientTool {
public class Method {
public static final String GET = "get";
public static final String POST = "post";
}
public static String invokeHttp(String method, String uri, Map<String, String> params)
throws IOException {
if (method.equals(HttpClientTool.Method.GET)) {
if (params == null) {
return invokeHttpGet(uri);
} else {
return invokeHttpGet(uri, params);
}
} else if (method.equals(HttpClientTool.Method.POST)) {
return invokeHttpPost(uri, params);
}
return null;
}
public static String invokeHttp(String method, URI uri, Map<String, String> params)
throws IOException, URISyntaxException {
if (method.equals(HttpClientTool.Method.GET)) {
if (params == null) {
return invokeHttpGet(uri);
} else {
return invokeHttpGet(uri, params);
}
} else if (method.equals(HttpClientTool.Method.POST)) {
return invokeHttpPost(uri, params);
}
return null;
}
public static String invokeHttp(String method, String scheme, String host, int port, String path, Map<String, String> params)
throws ClientProtocolException, URISyntaxException, IOException {
if (method.equals(HttpClientTool.Method.GET)) {
return invokeHttpGet(scheme, host, port, path, params);
} else if (method.equals(HttpClientTool.Method.POST)) {
return invokeHttpPost(scheme, host, port, path, params);
}
return null;
}
public static final String invokeHttpGet(String uri)
throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpGet httpGet = new HttpGet(uri);
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} finally {
release(client, response);
}
}
public static final String invokeHttpGet(String uri, Map<String, String> params)
throws ClientProtocolException, IOException {
String executeUri = uri + "?";
for (Map.Entry<String, String> entry : params.entrySet()) {
executeUri += entry.getKey() + "=" + entry.getValue() + "&";
}
executeUri = executeUri.substring(0, executeUri.length() - 1);
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpGet httpGet = new HttpGet(executeUri);
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} finally {
release(client, response);
}
}
public static final String invokeHttpGet(URI uri)
throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpGet httpGet = new HttpGet(uri);
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} finally {
release(client, response);
}
}
public static final String invokeHttpGet(URI uri, Map<String, String> params)
throws URISyntaxException, ClientProtocolException, IOException {
URIBuilder builder = new URIBuilder()
.setScheme(uri.getScheme())
.setHost(uri.getHost())
.setPort(uri.getPort())
.setPath(uri.getPath());
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.setParameter(entry.getKey(), entry.getValue());
}
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpGet httpGet = new HttpGet(builder.build());
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} finally {
release(client, response);
}
}
public static final String invokeHttpGet(String scheme, String host, int port, String path, Map<String, String> params)
throws URISyntaxException, ClientProtocolException, IOException {
URIBuilder builder = new URIBuilder()
.setScheme(scheme)
.setHost(host)
.setPort(port)
.setPath(path);
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.setParameter(entry.getKey(), entry.getValue());
}
URI uri = builder.build();
return invokeHttpGet(uri);
}
public static final String invokeHttpPost(String uri, Map<String, String> params)
throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(generatePostEntity(params));
try {
response = client.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} finally {
release(client, response);
}
}
public static final String invokeHttpPost(URI uri, Map<String, String> params)
throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(generatePostEntity(params));
try {
response = client.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity);
} finally {
release(client, response);
}
}
public static final String invokeHttpPost(String scheme, String host, int port, String path, Map<String, String> params)
throws URISyntaxException, ClientProtocolException, IOException {
URIBuilder builder = new URIBuilder()
.setScheme(scheme)
.setHost(host)
.setPort(port)
.setPath(path);
URI uri = builder.build();
return invokeHttpPost(uri, params);
}
private static UrlEncodedFormEntity generatePostEntity(Map<String, String> params) {
List<NameValuePair> pairs = new ArrayList<>();
for (Map.Entry<String, String> entry : params.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
return new UrlEncodedFormEntity(pairs, Consts.UTF_8);
}
private static void release(CloseableHttpClient client, CloseableHttpResponse response)
throws IOException {
if (client != null) {
client.close();
}
if (response != null) {
response.close();
}
}
}