分享

HttpURLConnection(访问网络)

 gearss 2016-05-21
  1. package com.example.lyftools.httptools;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.URL;  
  9.   
  10. public class HttpURLConnHelper {  
  11.     /** 
  12.      * 作用:实现网络访问文件,将获取到数据储存在文件流中 
  13.      *  
  14.      * @param url 
  15.      *            :访问网络的url地址 
  16.      * @return inputstream 
  17.      */  
  18.     public static InputStream loadFileFromURL(String url) {  
  19.         URL urlObj;  
  20.         BufferedInputStream bis = null;  
  21.         HttpURLConnection httpConn = null;  
  22.         try {  
  23.             // 创建url对象  
  24.             urlObj = new URL(url);  
  25.             // 创建HttpURLConnection对象,通过这个对象打开跟远程服务器之间的连接  
  26.             httpConn = (HttpURLConnection) urlObj.openConnection();  
  27.   
  28.             // 判断跟服务器的连接状态。如果是200,则说明连接正常,服务器有响应  
  29.             if (httpConn.getResponseCode() == 200) {  
  30.                 // 服务器有响应后,会将访问的url页面中的内容放进inputStream中,使用httpConn就可以获取到这个字节流  
  31.                 bis = new BufferedInputStream(httpConn.getInputStream());  
  32.                 return bis;  
  33.             }  
  34.         } catch (Exception e) {  
  35.             e.printStackTrace();  
  36.         } finally {  
  37.             try {  
  38.                 // 对流对象进行关闭,对Http连接对象进行关闭。以便释放资源。  
  39.                 bis.close();  
  40.                 httpConn.disconnect();  
  41.             } catch (IOException e) {  
  42.                 e.printStackTrace();  
  43.             }  
  44.   
  45.         }  
  46.         return null;  
  47.     }  
  48.   
  49.     /** 
  50.      * 作用:实现网络访问文件,将获取到的数据存在字节数组中 
  51.      *  
  52.      * @param url 
  53.      *            :访问网络的url地址 
  54.      * @return byte[] 
  55.      */  
  56.     public static byte[] loadByteFromURL(String url) {  
  57.         HttpURLConnection httpConn = null;  
  58.         BufferedInputStream bis = null;  
  59.         try {  
  60.             URL urlObj = new URL(url);  
  61.             httpConn = (HttpURLConnection) urlObj.openConnection();  
  62.             // httpConn.setRequestMethod("GET");  
  63.             // httpConn.setDoInput(true);  
  64.   
  65.             if (httpConn.getResponseCode() == 200) {  
  66.                 bis = new BufferedInputStream(httpConn.getInputStream());  
  67.                 return MyIOHelper.streamToByte(bis);  
  68.             }  
  69.         } catch (Exception e) {  
  70.             e.printStackTrace();  
  71.         } finally {  
  72.             try {  
  73.                 bis.close();  
  74.                 httpConn.disconnect();  
  75.             } catch (IOException e) {  
  76.                 e.printStackTrace();  
  77.             }  
  78.         }  
  79.         return null;  
  80.     }  
  81.   
  82.     /** 
  83.      * 作用:实现网络访问文件,先给服务器通过“POST”方式提交数据,再返回相应的数据 
  84.      *  
  85.      * @param url 
  86.      *            :访问网络的url地址 
  87.      * @param params 
  88.      *            :访问url时,需要传递给服务器的参数。格式为:username=wangxiangjun&password=abcde& 
  89.      *            qq=32432432 
  90.      *            为了防止传中文参数时出现编码问题。采用URLEncoder.encode()对含中文的字符串进行编码处理。 
  91.      *            服务器端会自动对进行过编码的字符串进行decode()解码。 
  92.      * @return byte[] 
  93.      */  
  94.     public static byte[] doPostSubmit(String url, String params) {  
  95.         BufferedOutputStream bos = null;  
  96.         BufferedInputStream bis = null;  
  97.         HttpURLConnection httpConn = null;  
  98.         try {  
  99.             URL urlObj = new URL(url);  
  100.             httpConn = (HttpURLConnection) urlObj.openConnection();  
  101.   
  102.             // 如果通过post方式给服务器传递数据,那么setDoOutput()必须设置为true。否则会异常。  
  103.             // 默认情况下setDoOutput()为false。  
  104.             // 其实也应该设置setDoInput(),但是因为setDoInput()默认为true。所以不一定要写。  
  105.   
  106.             httpConn.setDoOutput(true);  
  107.             httpConn.setRequestMethod("POST");  
  108.             // 设置请求方式。请求方式有两种:POST/GET。注意要全大写。  
  109.             // POST传递数据量大,数据更安全,地址栏中不会显示传输数据。  
  110.             // 而GET会将传输的数据暴露在地址栏中,传输的数据量大小有限制,相对POST不够安全。但是GET操作灵活简便。  
  111.   
  112.             // 判断是否要往服务器传递参数。如果不传递参数,那么就没有必要使用输出流了。  
  113.             if (params != null) {  
  114.                 byte[] data = params.getBytes();  
  115.                 bos = new BufferedOutputStream(httpConn.getOutputStream());  
  116.                 bos.write(data);  
  117.                 bos.flush();  
  118.             }  
  119.             // 判断访问网络的连接状态  
  120.             if (httpConn.getResponseCode() == 200) {  
  121.                 bis = new BufferedInputStream(httpConn.getInputStream());  
  122.                 // 将获取到的输入流转成字节数组  
  123.                 return MyIOHelper.streamToByte(bis);  
  124.             }  
  125.         } catch (Exception e) {  
  126.             e.printStackTrace();  
  127.         } finally {  
  128.             try {  
  129.                 if (bis != null) {  
  130.                     bis.close();  
  131.                 }  
  132.                 if (bos != null) {  
  133.                     bos.close();  
  134.                 }  
  135.                 httpConn.disconnect();  
  136.             } catch (IOException e) {  
  137.                 e.printStackTrace();  
  138.             }  
  139.   
  140.         }  
  141.         return null;  
  142.     }  
  143. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多