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();
}
}
文章来源地址https://www.toymoban.com/news/detail-634809.html
文章来源:https://www.toymoban.com/news/detail-634809.html
到了这里,关于JAVA实现postman中的digest认证请求的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!