Apache HttpComponents 简单说明

本文介绍了如何使用Apache HttpComponents库简化Java中的HTTP客户端编程,提供了与Socket编程相比更高效、功能丰富的封装,实现了客户端最新的HTTP标准和建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

尽管java.net包通过HTTP访问资源提供了基本的功能,它没有提供充分的灵活性,许多应用程序所需的功能。 HttpClient的,旨在填补这一空白,通过提供一个高效,同比增长日期,且功能丰富的封装,实现客户端最新的HTTP标准和建议。
利用HttpClient的设计为可扩展,同时提供强大的支持基本的HTTP协议,可能是感兴趣的任何建筑物知道HTTP的客户端应用程序,如Web浏览器,Web服务客户端,或系统或扩展HTTP协议的分布式通信。


用Java的Socket编程,其中不乏一些苦味的代码。例如往OutputStream中写入HTTP协义GET方法的请求头。示例

				Socket s=new Socket(host,80);
				OutputStream os=s.getOutputStream();
				
				PrintWriter osOut=new PrintWriter(os,true);
				//add request param
				osOut.println("GET "+path+"?"+param+" HTTP/1.1");
				osOut.println("Host: "+host+"");
				osOut.println("Accept-Language: en-us,en;q=0.5");
				osOut.println("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
				osOut.println("Accept-Encoding: gzip,deflate");
				osOut.println("Connection: Close");
				osOut.println();

这种代码还是需要程序员多少了解一下HTTP协义,Apache HttpComponents项目通过提供一个高效,功能丰富的封装,实现客户端最新的HTTP标准和建议。下面是示例代码

		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet("http://news.youkuaiyun.com/rss_news.html");
		try (CloseableHttpResponse response=httpclient.execute(httpGet)){
			HttpEntity entity = response.getEntity();

从输入流中获取的代码与Socket并无二样,org.apache.http.HttpEntity有一个getContent方法返回java.io.InputStream,因为目标的响应内容所用的字符编码并不一定与当前jvm的字符集编码一致。

			BufferedReader osIn=new BufferedReader(
					new InputStreamReader(is,Charset.forName("UTF-8")));
			//get response
			StringBuilder sb=new StringBuilder();
			String line=osIn.readLine();
			while(line!=null){
				sb.append(line);
				line=osIn.readLine();
			}

更多可以访问Apache HttpComponents tutorial


下面是一个简单的Socket GET方法代码

		try {
			//URL u=new URL("http://news.baidu.com/n?cmd=7&loc=4075&name=yantai&tn=rss");
			URL u=new URL("http://news.youkuaiyun.com/rss_news.html");
			String host=u.getHost();
			String path=u.getPath();
			String param=u.getQuery();
			
			try {
				Socket s=new Socket(host,80);
				OutputStream os=s.getOutputStream();
				
				PrintWriter osOut=new PrintWriter(os,true);
				BufferedReader osIn=new BufferedReader(
						new InputStreamReader(s.getInputStream(),Charset.forName("UTF-8")));
				
				//add request param
				osOut.println("GET "+path+"?"+param+" HTTP/1.1");
				osOut.println("Host: "+host+"");
				osOut.println("Accept-Language: en-us,en;q=0.5");
				osOut.println("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
				osOut.println("Accept-Encoding: gzip,deflate");
				osOut.println("Connection: Close");
				osOut.println();
				
				//get response
				StringBuilder sb=new StringBuilder();
				String line=osIn.readLine();
				while(line!=null){
					sb.append(line);
					line=osIn.readLine();
				}
				s.close();
				System.out.println(sb.toString());
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值