URL connection与HttpClientt的用法及区别

URL与HttpClent都是用来远程调用,它们的原理就是通过程序模拟浏览器的行为来获取或者像服务器提交数据。

URL是jdk自带的java.net.URL;

HttpClent是apache提供的,它们两个用法及结果都大同小异吧。


URL的用法:

<span style="font-size:14px;">public static String testURLconn(String httpUrl){
	String html = "";
	try {
		URL url = new URL(httpUrl);

		StringBuffer document = new StringBuffer();
		try {
			URLConnection urlCon = (HttpURLConnection) url.openConnection();
			
			urlCon.setConnectTimeout(1000*60);//设置超时
			
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(urlCon.getInputStream()));
			String Result = "";
			while ((Result = reader.readLine()) != null) {
				document.append(Result);
			}

			html = document.toString();
		} catch (Exception e) {
			e.printStackTrace();
			html = "服务未响应";
		}
	} catch (Exception e) {
		html = "不支持的协议";
		e.printStackTrace();
	}
	return html;
}</span>

HttpClent有两种,都是apache提供,需要添加相应的jar包

1.

org.apache.commons.httpclient.HttpClient类型:

需要下载添加jar包:

commons-httpclient-3.1.jar

commons-codec-1.3.jar

下载路径:http://download.csdn.net/detail/xiaosheng_papa/8690131

<span style="font-size:14px;">public static void testHttpClent(String url){
	try{
		HttpClient hc=new HttpClient();
		hc.setTimeout(1000*60);//设置响应超时
		//GetMethod method=new GetMethod(url);
		
		PostMethod method=new PostMethod(url);
		
		int state=hc.executeMethod(method);//响应状态
		
		//hc.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");//设置请求参数编码格式
		//hc.getParams().setHttpElementCharset("UTF-8");//设置请求参数编码格式
		//method.getParams().setHttpElementCharset("utf-8");//设置请求参数编码格式
		//method.getParams().setContentCharset("UTF-8");//设置请求参数编码格式
		
		System.out.println(state);
		System.out.println(method.getQueryString());//查询参数字符串
	
		System.out.println(method.getResponseBodyAsString());//响应内容
		
		//直接以字符串形式输出
		System.out.println(new String(method.getResponseBodyAsString()).getBytes("utf-8"));
		
		//以流的形式输出
		BufferedReader reader=new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); 
		String tmp=null; 
		String htmlRet=""; 
		while((tmp=reader.readLine())!=null){ 
			htmlRet+=tmp+"\r\n"; 
		} 
		System.out.println(htmlRet);
	}catch(Exception e){
		e.printStackTrace();
	}
}</span>

java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException

报这个错的话,是因为没有加入commons-codec-1.3.jar



2.

import org.apache.http.client.HttpClient类型:

需要下载添加jar包:

httpclient-4.3.1.jar

httpcore-4.3.jar

下载路径:http://download.csdn.net/detail/xiaosheng_papa/8690141

<span style="font-size:14px;">public static void testHttpClent(String url){
	try{
		HttpClient hc=new DefaultHttpClient();
		
		HttpGet request=new HttpGet(url);
		
		//HttpPost request=new HttpPost(url);
		
		HttpResponse response=hc.execute(request);
		System.out.println(response.getStatusLine().getStatusCode()+"\n");
		
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while((line = rd.readLine()) != null) {
        	System.out.println(line);
        }
        
        request.releaseConnection();

	}catch(Exception e){
		e.printStackTrace();
	}	
}</span>

URL与HttpClent的区别:










  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值