概述
private static int imcPort = Integer.parseInt(ConfigUtils.getNodeText("IMCPort"));
private static String imcIp = ConfigUtils.getNodeText("IMCIp");
static String imcUserName = ConfigUtils.getNodeText("IMCUserName");
static String imcPassword = ConfigUtils.getNodeText("IMCPassword");
static String url = "http://" + imcIp + ":" + imcPort + "/imcrs/";
CloseableHttpClient 设置用户名及密码授权信息
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpHost targetHost = new HttpHost(imcIp, imcPort, "http");
StringBuffer uri = new StringBuffer(url + "plat/res/devAppend/getAppendByDid/" + id);
HttpGet httpGet = new HttpGet(uri.toString().trim());
CredentialsProvider credsProvider = (CredentialsProvider) new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(imcIp, imcPort),
new UsernamePasswordCredentials(imcUserName, imcPassword));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
httpGet.addHeader("accept", "application/xml");
CloseableHttpResponse response =null;
response = httpclient.execute(
targetHost, httpGet, context);
int statusCode = response.getStatusLine().getStatusCode();
String xmlContent = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
引入的类如下
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
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.BasicScheme;
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.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
最后
以上就是知性曲奇为你收集整理的CloseableHttpClient 设置授权信息的全部内容,希望文章能够帮你解决CloseableHttpClient 设置授权信息所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复