CloseableHttpClient 设置授权信息

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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值