import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+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.entity.StringEntity;
+import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
+import org.apache.commons.codec.binary.Base64;
+
/*
* HttpClient post request
*/
HttpGet httpGet = null;
String result = null;
try{
- httpClient = HttpClientFactory.getSSLClientFactory();
+ httpClient = HttpClients.createDefault();
httpGet = new HttpGet(url);
-
+ Base64 token = new Base64();
+ String authenticationEncoding = new String(token.encode(("AAI:AAI").getBytes()));
+
+ httpGet.setHeader("Authorization", "Basic " + authenticationEncoding);
CloseableHttpResponse response = httpClient.execute(httpGet);
-
+ log.info("doGet sucess url ="+url);
try {
if(response != null){
HttpEntity resEntity = response.getEntity();
return result;
}
+ public static CloseableHttpClient createCloseableHttpClientWithBasicAuth(){
+ // 创建HttpClientBuilder
+ HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
+ // 设置BasicAuth
+ CredentialsProvider provider = new BasicCredentialsProvider();
+ // Create the authentication scope
+ AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
+ // Create credential pair,username and password
+ UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("AAI", "AAI");
+ // Inject the credentials
+ provider.setCredentials(scope, credentials);
+ // Set the default credentials provider
+ httpClientBuilder.setDefaultCredentialsProvider(provider);
+ // HttpClient
+ CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
+
+ return closeableHttpClient;
+ }
+
}
\ No newline at end of file