模拟发送HTTP请求:
1、采用客户端
PostMan,RestClient
2、采用代码方式实现模拟HTTP请求(HttpClient)
3、HTTPClient模拟Get请求
①目录展示
②导入HttpClient依赖
③GetClient
package com.zn; 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 java.io.IOException; public class GetClient { public static void main(String[] args) throws IOException { getRequest(); } //GET请求 public static void getRequest() throws IOException { //创建一个默认的连接 CloseableHttpClient client = HttpClients.createDefault(); //创建一个请求(以百度为例) HttpGet httpGet=new HttpGet("https://www.baidu.com/"); //执行请求获取响应的结果 CloseableHttpResponse response = client.execute(httpGet); //获取响应的状态码 System.out.println("服务返回的状态码:"+response.getStatusLine().getStatusCode()); //服务器正常响应 if (response.getStatusLine().getStatusCode()==200){ //获取响应结果 System.out.println(EntityUtils.toString(response.getEntity(),"UTF-8")); } //关闭结果对象 response.close(); //关闭连接 client.close(); } }
④效果展示
4、HTTPClient模拟Post请求
①目录展示
②ClientServlet
package com.zn; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/HttpClientServlet") public class ClientServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取数据 String username = request.getParameter("username"); System.out.println("接收的数据:"+username); response.setContentType("text/html;charset=utf-8"); response.getWriter().write("服务器正确接收数据!"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
③PostClient
package com.zn; 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.HttpGet; 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 java.io.IOException; import java.util.ArrayList; import java.util.List; public class PostClient { public static void main(String[] args) throws IOException { postRequest(); } //POST请求 public static void postRequest() throws IOException { //创建一个默认的连接 CloseableHttpClient client = HttpClients.createDefault(); //创建一个请求 HttpPost httpPost=new HttpPost("http://localhost:8080/HttpClientServlet"); //创建参数队列 List<NameValuePair> pairList=new ArrayList<>(); pairList.add(new BasicNameValuePair("username","张三")); //创建请求体,封装参数 UrlEncodedFormEntity entity=new UrlEncodedFormEntity(pairList,"UTF-8"); //将请求体交给当前请求 httpPost.setEntity(entity); //发送请求,接收结果 CloseableHttpResponse response = client.execute(httpPost); System.out.println("接收到的结果为:"+EntityUtils.toString(response.getEntity(),"UTF-8")); //关闭结果对象 response.close(); //关闭连接 client.close(); } }
④启动ClientServlet去访问HttpClientServlet
⑤启动PostClient