public static int sendPost(String url, String parameters,String username,String password ,String id) {
String result = "";
BufferedReader in = null;// 读取响应输入流
PrintWriter out = null;
StringBuffer sb = new StringBuffer();// 处理请求参数
String params = "";// 编码之后的参数
String ip= "";
String port= "";
int resCode = 404;
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(ip, port), // 请求地址 + 端口号
new UsernamePasswordCredentials(username, password));// 用户名 + 密码 (用于验证)
HttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
try {
SSLContext sslContext = SSLContextBuilder.create().useProtocol(SSLConnectionSocketFactory.SSL).loadTrustMaterial((x, y) -> true).build();
RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).setSSLContext(sslContext).setSSLHostnameVerifier((x, y) -> true).build();
} catch (Exception e) {
e.printStackTrace();
}
HttpPost postMethod = new HttpPost(url);// 请求详细地址(如:http://192.168.1.105:9000/MotorVehicles)
//根据不通要求自己添加头
postMethod.addHeader("User-Identify", id);
postMethod.addHeader("Content-Type", "application/json");
StringEntity s = new StringEntity(parameters);// 向后台传的json数据
s.setContentEncoding("utf-8");// 编码
postMethod.setEntity(s);
HttpResponse response = httpclient.execute(postMethod); // 执行POST方法
resCode = response.getStatusLine().getStatusCode();
System.out.println("resCode = " + resCode); // 获取响应码
// System.out.println("result = " + EntityUtils.toString(response.getEntity(), "utf-8")); // 获取响应内容
String resultfirst = EntityUtils.toString(response.getEntity(), "utf-8");
logger.info("result = " + resultfirst); // 获取响应内容
if(resCode == 401) {
String a = resultfirst;
// 组织参数,发起第二次请求
Header[] headers = response.getHeaders("WWW-Authenticate");
HeaderElement[] elements = headers[0].getElements();
String realm = null;
String qop = null;
String nonce = null;
String opaque = null;
String method = "POST";
String uri = url;
for (HeaderElement element : elements) {
if (element.getName().equals("Digest realm")) {
realm = element.getValue();
} else if (element.getName().equals("qop")) {
qop = element.getValue();
} else if (element.getName().equals("nonce")) {
nonce = element.getValue();
} else if (element.getName().equals("opaque")) {
opaque = element.getValue();
}
}
String a1 = username + ":" + realm + ":" + password;
String a2 = method + ":" + uri;
String response1 = null;
String nc = "00000001";
String cnonce = "uniview";
// 获取 Digest 这个字符串
String backString = response.getFirstHeader("WWW-Authenticate").getValue();
try {
response1 = DigestUtils.md5DigestAsHex((DigestUtils.md5DigestAsHex(a1.getBytes("UTF-8")) + ":" + nonce + ":" + nc
+ ":" + "uniview" + ":" + qop + ":" + DigestUtils.md5DigestAsHex(a2.getBytes("UTF-8"))).getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
System.out.println("md5加密异常"+e);
}
postMethod.addHeader("Authorization", backString + ",username=\"" + username + "\"" + ",realm=\"" + realm + "\""
+ ",nonce=\"" + nonce + "\"" + ",uri=\"" + uri + "\"" + ",qop=\"" + qop + "\"" + ",nc=\"" + nc + "\""
+ ",cnonce=\"" + cnonce + "\"" + ",response=\"" + response1 + "\"" + ",opaque=\"" + opaque);
// 发送第二次请求
response = httpclient.execute(postMethod);
resCode = response.getStatusLine().getStatusCode();//获取返回响应码
System.out.println(resCode);
if (HttpStatus.SC_OK == resCode) {
logger.info("result = " + EntityUtils.toString(response.getEntity(), "utf-8")); // 获取响应内容
return resCode;
} else {
}
} else {
}
} catch (Exception e) {
System.out.println(e);
}
return resCode;
}
``
对应 的xml
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.1</version>
<exclusions>
<exclusion>
<artifactId>elasticsearch-rest-client</artifactId>
<groupId>org.elasticsearch.client</groupId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<artifactId>elasticsearch-rest-client</artifactId>
<groupId>org.elasticsearch.client</groupId>
<version>6.3.1</version>
</dependency>
<dependency>
<artifactId>elasticsearch</artifactId>
<groupId>org.elasticsearch</groupId>
<version>6.3.1</version>
</dependency>
java模拟Digest.认证
最新推荐文章于 2024-06-21 01:37:04 发布