您的位置:首页 > 理论基础 > 计算机网络

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();
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: