URL connection与HttpClientt的用法及差异
URL connection与HttpClientt的用法及区别
HttpClent有两种,都是apache提供,需要添加相应的jar包
2.
URL与HttpClent的区别:
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.****.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.****.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的区别: