服务端模拟HTTP请求
通过JAVA代码进行HTTP请求的发送
1.准备依赖
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency>
2.创建post和get方式请求的方法
package com.ty.http; 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.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 MyHttpClient { private static void myPost() throws IOException { //建立连接请求 CloseableHttpClient httpClient = HttpClients.createDefault(); //创建post请求 HttpPost httpPost = new HttpPost("http://eureka7002.com:6061/myServlet"); //创建参数队列 List<NameValuePair> nameValuePairList=new ArrayList<>(); //准备请求参数队列 nameValuePairList.add(new BasicNameValuePair("userName","你好骚呀")); // UrlEncodedFormEntity urlEncodedFormEntity=new UrlEncodedFormEntity(nameValuePairList,"UTF-8"); //讲实体封装到请求当中 httpPost.setEntity(urlEncodedFormEntity); //发送请求 CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity!=null){ System.out.println("接收到的响应信息:--------"+EntityUtils.toString(entity,"UTF-8")); } //资源释放 response.close(); httpClient.close(); } private static void myGet() throws IOException { //建立连接请求 CloseableHttpClient httpClient = HttpClients.createDefault(); //创建get请求 HttpGet get=new HttpGet("https://www.baidu.com/"); CloseableHttpResponse response = httpClient.execute(get); int statusCode = response.getStatusLine().getStatusCode(); //响应成功在操作 if (statusCode==200) { //获取响应实体 HttpEntity entity = response.getEntity(); //打印响应内容长度 System.out.println("响应文本长度:"+entity.getContentLength()); //打印响应内容 System.out.println("响应内容如下:\n"+EntityUtils.toString(entity,"UTF-8")); } System.out.println("--------------------------响应内容打印完毕"); //释放资源 response.close(); httpClient.close(); } public static void main(String[] args) { try { // myGet(); myPost(); } catch (IOException e) { e.printStackTrace(); } } }
4.get测试百度返回结果
5.post测试本机的另一个服务
5.1给项目准备依赖
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency>
5.2创建一个Servlet,接收一个参数,返回json串
package com.ty.servlet; import com.alibaba.fastjson.JSON; 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("/myServlet") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String userName = req.getParameter("userName"); resp.setHeader("Content-type", "text/html;charset=UTF-8"); System.out.println("接受到的数据:"+userName); String json="成功响应"; resp.getWriter().write(JSON.toJSONString(json)); } }