0


JAVA实现postman中的digest认证请求

1.生成 digestHttpClient 
​​​​​​​/**
 * @param username:
 * @param password:
 * @param host:
 * @param port:
 * @return HttpClient
 * @author cuipengfei
 * @description TODO httpclient 4.3 后  ,生成认证的httpclient
 * @date 2022/6/22 18:33
 */
public static HttpClient digestHttpClient( String username,  String password, String host,  Integer port){
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(StringUtils.isBlank(host) ? AuthScope.ANY_HOST : host, port == null ? AuthScope.ANY_PORT : port),
            new UsernamePasswordCredentials(username,password));
    return HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
}

public static HttpClient httpClient(){
    return HttpClients.custom().build();
}
2.测试
    public static void main(String[] args){

        HttpClient defaultHttpClient = httpClient();
        HttpClient digestHttpClient = digestHttpClient("ApiAdmin","Hx123456",null,null);

        HttpPut httpPut = new HttpPut("http://localhost:80/SDCAPI/V1.0/Streaming/Sessions?UUID=6f95a0ae-7bde-0730-b43e-277dcfbd3dcd&SessionId=34");
        httpPut.setHeader("Content-Type", "application/json");
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("videoPt",96);
        jsonObject2.put("audioPt",15);
        jsonObject2.put("metaPt",107);
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("media",jsonObject2.toString());

        try {
            StringEntity stringEntity = new StringEntity(jsonObject3.toString());
            stringEntity.setContentType("application/json");
            httpPut.setEntity(stringEntity);
            System.out.println("=============digest auth=====================");
            System.out.println(EntityUtils.toString(digestHttpClient.execute(httpPut).getEntity()));
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
标签: java

本文转载自: https://blog.csdn.net/weixin_47413955/article/details/125596258
版权归原作者 学习记录1 所有, 如有侵权,请联系我们删除。

“JAVA实现postman中的digest认证请求”的评论:

还没有评论