天天看點

java http 摘要認證_HTTP 基本認證&摘要認證(java方式具體實作get請求)

maps = new HashMap();

try {

URI serverURI = new URI(url);

CredentialsProvider credsProvider = new BasicCredentialsProvider();

credsProvider.setCredentials(new AuthScope("192.168.201.210", 8080),

new UsernamePasswordCredentials(username, password));

httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();

HttpGet get = new HttpGet(url);

// 構造消息頭

get.addHeader("Content-type","application/json; charset=utf-8");

get.setHeader("Accept", "application/json");

HttpResponse response = httpClient.execute(get);

HttpEntity entity = null;

//String responseContent = null;

entity = response.getEntity();

result = EntityUtils.toString(entity, "UTF-8");

maps.put("result", result);

//String result = EntityUtils.toString(response.getEntity());

System.out.println("傳回的消息:" + result);

System.out.println("傳回的狀态: " + response.getStatusLine().getStatusCode());

System.out.println("傳回的類型: " + response.getEntity().getContentType());

sc = response.getStatusLine().getStatusCode();

EntityUtils.consume(response.getEntity());

httpClient.close();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (httpClient != null) {

try {

httpClient.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return maps;

}