`

java http请求(一) ---- 工具

    博客分类:
  • JAVA
阅读更多

http请求(一) ---- 工具

  2  import  java.io.ByteArrayOutputStream;
  3  import  java.io.DataOutputStream;
  4  import  java.io.InputStream;
  5  import  java.net.HttpURLConnection;
  6  import  java.net.URL;
  7  import  java.net.URLEncoder;
  8  import  java.util.Map;
  9  import  android.util.Log;
 10  
 11  public   class  NetUtil {
 12  
 13       private   static   final  String TAG  =   " NetUtil " ;
 14       private   static   final   int  RESPONSE_OK  =   200 ;
 15      
 16       public   static  InputStream sendPostRequest(String urlPath,
 17              Map < String, String >  params, String encoding)  throws  Exception {
 18           //  String param = "method=save&id=24&name="
 19           //  + URLEncoder.encode("大圆", "UTF-8");
 20          StringBuilder sb  =   new  StringBuilder();
 21           for  (Map.Entry < String, String >  entry : params.entrySet()) {
 22              sb.append(entry.getKey()).append( " = " )
 23                      .append(URLEncoder.encode(entry.getValue(), encoding))
 24                      .append( " & " );
 25          }
 26          sb.deleteCharAt(sb.lastIndexOf( " & " ));
 27           byte [] data  =  sb.toString().getBytes();
 28          URL url  =   new  URL(urlPath);
 29          HttpURLConnection conn  =  (HttpURLConnection) url.openConnection();
 30          conn.setRequestMethod( " POST " );
 31          conn.setReadTimeout( 5   *   1000 );
 32          conn.setDoOutput( true );  //  发送POST请求, 必须设置允许输出
 33          conn.setUseCaches( false );
 34          conn.setRequestProperty( " Connection " " Keep-Alive " );  //  维持长链接
 35          conn.setRequestProperty( " Charset " " UTF-8 " );
 36           //  设置输入参数的大小,把参数转化为字节数组
 37          conn.setRequestProperty( " Content-Length " , String.valueOf(data.length));
 38           //  设置数据类型
 39          conn.setRequestProperty( " Content-Type " ,
 40                   " application/x-www-form-urlencoded " );
 41          
 42          DataOutputStream outStream  =   new  DataOutputStream(
 43                  conn.getOutputStream());
 44          outStream.write(data);
 45          outStream.flush();
 46          outStream.close();
 47           if  (conn.getResponseCode()  ==  RESPONSE_OK) {
 48               return  conn.getInputStream();
 49          }
 50           return   null ;
 51      }
 52  
 53       /*
 54       * 得到http返回的输入流,并且转化成String
 55        */
 56       public   static  String getTextContent(String urlPath, String encoding)
 57               throws  Exception {
 58          URL url  =   new  URL(urlPath);
 59          HttpURLConnection conn  =  (HttpURLConnection) url.openConnection();
 60          conn.setRequestMethod( " GET " );
 61          conn.setReadTimeout( 5   *   1000 );
 62           if  (conn.getResponseCode()  ==  RESPONSE_OK) {
 63              InputStream inStream  =  conn.getInputStream();
 64               byte [] data  =  readStream(inStream);
 65              System.out.println( new  String(data, encoding));
 66               return   new  String(data, encoding);
 67          }
 68           return   null ;
 69      }
 70  
 71       //  读取数据
 72       public   static   byte [] readStream(InputStream inStream)  throws  Exception {
 73          ByteArrayOutputStream outStream  =   new  ByteArrayOutputStream();
 74           byte [] buffer  =   new   byte [ 2048 ];
 75           int  length  =   - 1 ;
 76           while  ((length  =  (inStream.read(buffer)))  !=   - 1 ) {
 77              outStream.write(buffer,  0 , length);
 78          }
 79          outStream.close();
 80           return  outStream.toByteArray();
 81      }
 82  
 83       //  直接返回http得到的输入流
 84       public   static  InputStream getStreamContent(String urlPath, String encoding)
 85               throws  Exception {
 86          InputStream inStream  =   null ;
 87          URL url  =   new  URL(urlPath);
 88          HttpURLConnection conn  =  (HttpURLConnection) url.openConnection();
 89          conn.setRequestMethod( " GET " );
 90          conn.setReadTimeout( 5   *   1000 );
 91           if  (conn.getResponseCode()  ==  RESPONSE_OK) {
 92              inStream  =  conn.getInputStream();
 93          }
 94           return  inStream;
 95      }
 96  
 97       public   static   void  print(String tag, String msg) {
 98          Log.d(tag, msg);
 99      }
100  }
101

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics