展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

httpclient发送Authorization Basic请求

  • http client发送请求Authorization Basic + application/json
  • 代码实现
# 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>
    <!-- httpclient -->
    <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";
    }

}
  • 实现效果
# test.http

@hostname = 174.235.678.123
@port=8081
@contentType=application/json
@userName=admin
@password=public

POST http://{{hostname}}:{{port}}/api/v4/auth_username HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}

{
    "username": "user1",
    "password": "123456"
}
posted @ 2022-07-22 15:34  DogLeftover  阅读(88)  评论(0编辑  收藏  举报