JAVA: httpclient 详细说明——第四章;




httpclient 具体解释——第一章。

httpclient 具体解释——第二章;

httpclient 具体解释——第三章;

httpclient 具体解释——第四章; 

httpclient 具体解释——第五章;

httpclient 具体解释——第六章;

httpclient 具体解释——第七章;

相对于httpurlconnection ,httpclient更加丰富,也更加强大,当中apache有两个项目都是httpclient。一个是commonts包下的,这个是通用的,更专业的是org.apache.http.包下的。所以我一般用后者;


httpclient能够处理长连接,保存会话,重连接,以及请求过滤器,连接重用等等...


以下是測试代码(所有总结来自官方文档。以及翻译)


需要下载核心包:httpclient-4.3.4.jar ,也可在官网下载:http://hc.apache.org/downloads.cgi



/**
	 * HTTP认证:
	 * 
	 * HTTPclient支持http标准认证,也支持其它认证,如NTLM和SPNEGO。
	 */
	private static void test17(){
		
		//最简单的明文username password认证。
		UsernamePasswordCredentials creds = new UsernamePasswordCredentials("user", "pwd");
		System.out.println(creds.getUserPrincipal().getName());
		System.out.println(creds.getPassword());
		
		/**
		 * NTCredentials是微软的windows系统使用的一种凭据。包括username、password,还包括一系列其它的属性。
		 * 比方用户所在的域名。在Microsoft Windows的网络环境中,同一个用户能够属于不同的域,所以他也就有不同的凭据。
		 * workstation:本机的计算机名
		 */
		NTCredentials ntcreds = new NTCredentials("user", "pwd", "workstation", "domain");
		System.out.println(ntcreds.getUserPrincipal().getName());//输出   DOMAIN/user
		System.out.println(ntcreds.getPassword());
		
		
	}
	
	/**
	 * 凭证提供者(CredentialsProvider)内含一套特定的凭证,需要哪种凭证时,CredentialsProvider就能获得相应的凭证。
	 * 获取凭证的时候,能够模糊的指定主机名、port号、realm和认证方案。
	 * CredentialsProvider会筛选出一个最佳匹配方案。
	 */
	private static void test18() {

		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		
		credsProvider.setCredentials(new AuthScope("somehost",AuthScope.ANY_PORT),
									 new UsernamePasswordCredentials("u1", "p1"));
		credsProvider.setCredentials(new AuthScope("somehost", 8080),
									 new UsernamePasswordCredentials("u2", "p2"));
		credsProvider.setCredentials(new AuthScope("otherhost", 8080,AuthScope.ANY_REALM, "ntlm"), 
									 new UsernamePasswordCredentials("u3", "p3"));
		
		System.out.println(credsProvider.getCredentials(new AuthScope(
				"somehost", 80, "realm", "basic")));
		System.out.println(credsProvider.getCredentials(new AuthScope(
				"somehost", 8080, "realm", "basic")));
		System.out.println(credsProvider.getCredentials(new AuthScope(
				"otherhost", 8080, "realm", "basic")));
		System.out.println(credsProvider.getCredentials(new AuthScope(
				"otherhost", 8080, null, "ntlm")));
		
		/**
		 *   输出:
		 *  [principal: u1]
			[principal: u2]
			null
			[principal: u3]
		 */
	}
	
	
	
	private static void test19() throws ClientProtocolException, IOException {
		
//		CloseableHttpClient httpclient = HttpClients.createDefault();
//		HttpClientContext context = HttpClientContext.create();
//		
//		HttpHost targetHost = new HttpHost("webservice.webxml.com.cn", 80, "http");
//
//		//认证提供者
//		CredentialsProvider credsProvider = new BasicCredentialsProvider();
//		credsProvider.setCredentials(new AuthScope(targetHost.getHostName(),targetHost.getPort()),
//									 new UsernamePasswordCredentials("root", "root"));
//		
//		//基础认证缓存
//		AuthCache authCache = new BasicAuthCache();
//		
//		context.setCredentialsProvider(credsProvider);
//		context.setAuthCache(authCache);
//		
//		HttpGet httpget = new HttpGet("/WebServices/MobileCodeWS.asmx/getDatabaseInfo");
//		
//		CloseableHttpResponse response = httpclient.execute(targetHost,httpget, context);
//		
//		AuthState proxyAuthState = context.getProxyAuthState();
//		
//		System.out.println("Proxy auth state: " + proxyAuthState.getState());
//		System.out.println("Proxy auth scheme: "+ proxyAuthState.getAuthScheme());
//		System.out.println("Proxy auth credentials: "+ proxyAuthState.getCredentials());
//		AuthState targetAuthState = context.getTargetAuthState();
//		System.out.println("Target auth state: " + targetAuthState.getState());
//		System.out.println("Target auth scheme: "+ targetAuthState.getAuthScheme());
//		System.out.println("Target auth credentials: "+ targetAuthState.getCredentials());
		/**
		 * 	Proxy auth state: UNCHALLENGED
			Proxy auth scheme: null
			Proxy auth credentials: null
			Target auth state: UNCHALLENGED
			Target auth scheme: null
			Target auth credentials: null
		 */
	}
	
	
	/**
	 * HttpClientContext 设置抢先认证  和  认证提供者:
	 * 
	 * 在HTTP中。基本认证是一种用来同意Web浏览器或其它客户端程序在请求时提供以username和口令形式的凭证。
	 * 
	 * 一般http basic认证,首先登录server。 然后server会返回401码让客户端输入username和password,客户端把usernamepassword进行BASE64加密。
	 * 
	 * 最后把加密后的username和password发送给server进行验证。 
	 * 
	 * 抢先验证则省略了前几部,直接发送BASE64位密文,进行验证。但存在风险,慎用。
	 */
	private static void test20() throws ClientProtocolException, IOException {
		
		
		HttpClientContext context = HttpClientContext.create();
		CloseableHttpClient httpclient = HttpClients.createDefault();
		//配置主机 。 port可任意填写
		HttpHost targetHost = new HttpHost("webservice.webxml.com.cn", 80, "http");
		//认证提供者
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		
		credsProvider.setCredentials(
				new AuthScope(targetHost.getHostName(),targetHost.getPort()), 
				new UsernamePasswordCredentials("username", "password"));
		
		AuthCache authCache = new BasicAuthCache();
		
		BasicScheme basicAuth = new BasicScheme();
		authCache.put(targetHost, basicAuth);
		
		//提前填充认证信息缓存到上下文中,这样,以这个上下文运行的方法。就会使用抢先认证。

可能会出错 context.setAuthCache(authCache); context.setCredentialsProvider(credsProvider); HttpGet httpget = new HttpGet("/WebServices/MobileCodeWS.asmx/getDatabaseInfo"); CloseableHttpResponse response = httpclient.execute(targetHost,httpget, context); try { HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity); System.out.println(result); } finally { response.close(); } } /** * NTLM连接认证: * * windows提供的一套安全、复杂的、有状态的协议。 * * 相比Basic和Digest认证。NTLM认证要明显需要很多其它的计算开销,性能影响也比較大。

* * 也就是说,NTLM连接一旦建立,用户的身份就会在其整个生命周期和它相关联。 */ private static void test21() throws ClientProtocolException, IOException { // 确保使用同一个上下文 HttpClientContext context = HttpClientContext.create(); CloseableHttpClient httpclient = HttpClients.createDefault(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); //构建具体nt认证信息:NTCredentials 參数1:username 2:password 3:本机名 3:domain域 credsProvider.setCredentials(new AuthScope("somehost",AuthScope.ANY_PORT) , new NTCredentials("username", "password", "myworkstation", "domain")); context.setCredentialsProvider(credsProvider); HttpHost target = new HttpHost("somehost", 80, "http"); HttpGet httpget = new HttpGet("/"); CloseableHttpResponse response1 = httpclient.execute(target, httpget, context); try { HttpEntity entity1 = response1.getEntity(); System.out.println(EntityUtils.toString(entity1,"GB2312")); } finally { response1.close(); } HttpPost httppost = new HttpPost("/"); httppost.setEntity(new StringEntity("lots and lots of data")); CloseableHttpResponse response2 = httpclient.execute(target, httppost, context); try { HttpEntity entity2 = response2.getEntity(); } finally { response2.close(); } }



版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/lcchuguo/p/4807249.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值