- 发送post请求,并使用Authorization Basic + application/json
| @GetMapping("/test4") |
| @ResponseBody |
| public String test4() throws IOException { |
| String url ="http://192.168.96.96:8081/api/v4/auth_username"; |
| Map<String, String> prarms = new HashMap<>(); |
| prarms.put("username","user2"); |
| prarms.put("password", "123456"); |
| String jsonPrarms = JSON.toJSONString(prarms); |
| CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
| HttpPost httpPost = new HttpPost(url); |
| String encoding = DatatypeConverter.printBase64Binary("admin:public".getBytes("UTF-8")); |
| httpPost.setHeader("Authorization", "Basic " + encoding); |
| HttpEntity entityParam = new StringEntity(jsonPrarms, ContentType.create("application/json", "UTF-8")); |
| httpPost.setEntity(entityParam); |
| HttpResponse response = httpClient.execute(httpPost); |
| StatusLine statusLine = response.getStatusLine(); |
| int responseCode = statusLine.getStatusCode(); |
| if (responseCode == 200) { |
| |
| HttpEntity entity = response.getEntity(); |
| InputStream input = entity.getContent(); |
| BufferedReader br = new BufferedReader(new InputStreamReader(input,"utf-8")); |
| String str1 = br.readLine(); |
| System.out.println("服务器的响应是:" + str1); |
| br.close(); |
| input.close(); |
| } else { |
| System.out.println("响应失败"); |
| } |
| |
| return "success"; |
| } |
| |
| # 等同于如下: |
| @hostname = 192.168.96.96 |
| @port=8081 |
| @contentType=application/json |
| @userName=admin |
| @password=public |
| |
| POST http: |
| Content-Type: {{contentType}} |
| Authorization: Basic {{userName}}:{{password}} |
| |
| { |
| "username": "user1", |
| "password": "123456" |
| } |
| @GetMapping("/test5") |
| @ResponseBody |
| public String test5() throws IOException { |
| String url ="http://192.168.96.96:8081/api/v4/auth_username/user1"; |
| String encoding = DatatypeConverter.printBase64Binary("admin:public".getBytes("UTF-8")); |
| doDelete(url, encoding, ""); |
| |
| return "success"; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static String doDelete(String url, String encoding, String jsonStr) { |
| CloseableHttpClient httpClient = HttpClients.createDefault(); |
| HttpDelete httpDelete = new HttpDelete(url); |
| RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build(); |
| httpDelete.setConfig(requestConfig); |
| httpDelete.setHeader("Content-type", "application/json"); |
| httpDelete.setHeader("DataEncoding", "UTF-8"); |
| httpDelete.setHeader("Authorization", "Basic " + encoding); |
| CloseableHttpResponse httpResponse = null; |
| try { |
| httpResponse = httpClient.execute(httpDelete); |
| HttpEntity entity = httpResponse.getEntity(); |
| String result = EntityUtils.toString(entity); |
| return result; |
| } catch (ClientProtocolException e) { |
| |
| e.printStackTrace(); |
| } catch (IOException e) { |
| |
| e.printStackTrace(); |
| } finally { |
| if (httpResponse != null) { |
| try { |
| httpResponse.close(); |
| } catch (IOException e) { |
| |
| e.printStackTrace(); |
| } |
| } |
| if (null != httpClient) { |
| try { |
| httpClient.close(); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| return null; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构