| # pom.xml |
| <dependencies> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-web</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-devtools</artifactId> |
| <scope>runtime</scope> |
| <optional>true</optional> |
| </dependency> |
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| <optional>true</optional> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-test</artifactId> |
| <scope>test</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.httpcomponents</groupId> |
| <artifactId>httpclient</artifactId> |
| <version>4.5.13</version> |
| </dependency> |
| <dependency> |
| <groupId>com.alibaba</groupId> |
| <artifactId>fastjson</artifactId> |
| <version>1.2.72</version> |
| </dependency> |
| <dependency> |
| <groupId>jakarta.xml.bind</groupId> |
| <artifactId>jakarta.xml.bind-api</artifactId> |
| </dependency> |
| </dependencies> |
| |
| # yml配置 |
| server: |
| port: 8080 |
| spring: |
| application: |
| name: demo01 |
| logging: |
| level: |
| com: |
| ychen: |
| demo01: debug |
| |
| # 具体实现 |
| import com.alibaba.fastjson.JSON; |
| import org.apache.http.HttpEntity; |
| import org.apache.http.HttpResponse; |
| import org.apache.http.StatusLine; |
| 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.entity.ContentType; |
| import org.apache.http.entity.StringEntity; |
| import org.apache.http.impl.client.CloseableHttpClient; |
| import org.apache.http.impl.client.HttpClientBuilder; |
| import org.apache.http.util.EntityUtils; |
| import org.springframework.web.bind.annotation.GetMapping; |
| import org.springframework.web.bind.annotation.ResponseBody; |
| import org.springframework.web.bind.annotation.RestController; |
| import javax.xml.bind.DatatypeConverter; |
| import java.io.*; |
| import java.net.URLEncoder; |
| import java.nio.charset.StandardCharsets; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| @RestController |
| public class TestController { |
| |
| @GetMapping("/test") |
| @ResponseBody |
| public String test() throws IOException { |
| String url ="http://174.235.678.123: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")); //username password 自行修改 中间":"不可少 |
| httpPost.setHeader("Authorization", "Basic " + encoding); |
| HttpEntity entityParam = new StringEntity(jsonPrarms, ContentType.create("application/json", "UTF-8")); //这里的“application/json” 可以更换因为本人是传的json参数所以用的这个 |
| httpPost.setEntity(entityParam); //把参数添加到post请求 |
| 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("响应失败"); |
| } |
| // end |
| return "success"; |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-07-22 ubuntu入门