httpclient4.4 http摘要认证请求
2017-05-19 15:24
176 查看
一、
package net.like.pro.common.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import net.sf.json.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpStatus; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.AuthCache; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.entity.StringEntity; import org.apache.http.impl.auth.DigestScheme; import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; public class HttpDigestUtils { public static String sendHttpClientAuth(String url, String username, String password, JSONObject parm){ CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = null; BufferedReader in = null; try { URI serverUri = new URI(url); //host HttpHost httpHost = new HttpHost(serverUri.getHost(), serverUri.getPort()); //post HttpPost post = new HttpPost(url); //表单提交 // List<NameValuePair> parms = new ArrayList<NameValuePair>(); // parms.add(new BasicNameValuePair("data", parm.toString())); // UrlEncodedFormEntity httpEntity = new UrlEncodedFormEntity(parms); // httpEntity.setContentType("application/json;charset=utf-8"); // httpEntity.setContentEncoding("UTF-8"); // post.setEntity(httpEntity); // post.addHeader("accept", "*/*"); // post.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // post.addHeader("connection", "close"); //json提交 StringEntity stringEntity = new StringEntity(parm.toString(),"UTF-8");//解决中文乱码问题 stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); stringEntity.setContentType("application/json"); post.setEntity(stringEntity); // 创建认证缓存 AuthCache authCache = new BasicAuthCache(); // 创建基础认证机制 添加到缓存 DigestScheme digestAuth = new DigestScheme(); authCache.put(httpHost, digestAuth); //context //基础凭证提供器,明文传输数据 CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(httpHost.getHostName(), httpHost.getPort()), new UsernamePasswordCredentials(username,password)); // 将认证缓存添加到执行环境中 即预填充 HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache); response = client.execute(post, context); System.out.println(response.getStatusLine()); StringBuilder result = new StringBuilder(); if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity entity = response.getEntity(); if(entity!=null){ in = new BufferedReader(new InputStreamReader(entity.getContent())); String line = null; while((line = in.readLine())!=null){ System.out.println(result.toString()); result.append(line); } } } System.out.println(result.toString()); return ""; }catch(Exception e){ e.printStackTrace(); return "httpclient异常:"+e.getMessage(); }finally{ try { if(response!=null){ response.close(); } if (in != null) in.close(); if(client!=null){ client.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
相关文章推荐
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结Atitit HT
- 详解HTTP中的摘要认证机制
- 详解HTTP中的摘要认证机制
- nginx配置将http请求转发支持ssl双向认证https请求的正向代理
- 前端学HTTP之摘要认证
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结
- WebApi接口安全认证——HTTP之摘要认证
- HTTP摘要认证的客户端实现
- 基于SpringMVC的HTTP摘要认证的服务器端实现
- ASIHTTPRequest实现https双向认证请求
- HTTP摘要认证
- 详解HTTP中的摘要认证机制
- HttpClient请求https认证问题
- ASIHTTPRequest实现https双向认证请求
- 一种基于改进的HTTP摘要认证的SIP安全机制
- HTTP摘要认证方式分析与验证
- java 发送带Basic Auth认证的http post请求实例代码
- Java模拟http上传文件请求(HttpURLConnection,HttpClient4.4,RestTemplate)
- HttpClient4.4 模仿登陆及维持同一session 请求
- nginx配置将http请求转发支持ssl双向认证https请求的正向代理