在沒有頁面的情況下來獲取接口返回的數據(一般都是為JSON),我們可以通過一些工具模擬HTTP請求
服務端模擬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)); } }
5.3啟動服務,發送post請求,結果如下
客戶端工具模擬HTTP請求
PostMan發送get請求百度
發送post請求