当写好了WEB程序,使用Java客户端访问,最好使用HttpComponents组件。在apche上下载,配置到项目中。
范例:定义一个处理用户请求的Servlet程序
package cn.ren.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
@WebServlet("/hello")
public class HttpServlet extends javax.servlet.http.HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("name = " + request.getParameter("name"));
System.out.println("***" + request.getParameter("name")); // 在后台做个打印
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
范例:定义一个Get请求:
package cn.ren.demo;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
public class HttpClientGetDemo {
public static void main(String[] args) throws Exception {
String url = "http://localhost/HttpServlet/hello?name=ren" ;
CloseableHttpClient httpClient = HttpClients.createDefault() ; // 创建一个Http访问客户端
HttpGet httpGet = new HttpGet(url) ; // GET请求
CloseableHttpResponse reponse = httpClient.execute(httpGet) ; // 发送GET请求
System.out.println(reponse.getEntity());
System.out.println(reponse.getCode());
System.out.println(EntityUtils.toString(reponse.getEntity()));
}
}
范例:使用HttpClient发送POST请求
在HelloServlet里面追加一个处理POST请求的方法:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String msg = request.getParameter("msg") ;
System.out.println("### POST请求" + msg); // 后台打印
response.getWriter().print("<h1>" + msg + "</h1>");
}
定义POST请求处理:
package cn.ren.demo;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
public class HttpClientPostDemo {
public static void main(String[] args) throws Exception {
String url = "http://localhost/HttpServlet/hello";
CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建一个Http访问客户端
HttpPost httpPost = new HttpPost(url); // Post请求
// 需要将所有的参数进行一个封装处理
List<NameValuePair> allParams = new ArrayList<NameValuePair>();
allParams.add(new BasicNameValuePair("msg", "世界,你好")); // 追加传递参数
allParams.add(new BasicNameValuePair("mid", "renjava"));
// 由于传递了中文,所以需要对中文进行编码控制
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(allParams); // 设置编码
httpPost.setEntity(entity); // 将发送的数据与POST对象绑定在一起
CloseableHttpResponse response = httpClient.execute(httpPost); // 发送Post请求
System.out.println(response.getEntity());
System.out.println(response.getCode());
System.out.println(EntityUtils.toString(response.getEntity()));
}
}