zoukankan      html  css  js  c++  java
  • java Http消息传递之POST和GET两种方法--通过实用工具类来获取服务器资源

    实现该方法需要导入一些jar包

    可以去一下地址下载:

    http://pan.baidu.com/s/1hqrJF7m

    /**
    * 实用工具类来获取服务器资源
    *
    * get方法传送数据
    *
    * 1、通过path设定传送方式
    * 2、创建客户端
    * 3、得到输入流
    * 4、读取流准备工作
    * 5、读取并写入
    * @throws IOException
    * @throws ClientProtocolException
    *
    */

     1     public static String getHttpResult(String path) throws ClientProtocolException, IOException{
     2     /*1、通过path设定传送方式*/
     3         
     4         HttpGet get=new HttpGet(path);
     5     /*2、创建客户端*/
     6         HttpClient client=new DefaultHttpClient();
     7         //通过get方式发送数据给服务器
     8         HttpResponse response=client.execute(get);
     9     /*3、得到输入流*/
    10         if(response.getStatusLine().getStatusCode()==200){
    11             InputStream in=response.getEntity().getContent();
    12             
    13     /*4、读取流准备工作*/
    14             ByteArrayOutputStream bos=new ByteArrayOutputStream();
    15             byte[]arr=new byte [1024];
    16             int len=0;
    17             
    18     /*5、读取并写入*/
    19             while((len=in.read(arr))!=-1){
    20                 bos.write(arr, 0, len);
    21             }
    22             byte[]b=bos.toByteArray();
    23             return new String(b,0,b.length);
    24         }
    25         
    26         
    27         
    28         return null;
    29     }

    /**
    * 实用工具类来获取服务器资源
    *
    * Post方法传送数据
    *
    * 1、通过path设定传送方式
    * 2、创建客户端
    * 3、得到输入流
    * 4、读取流准备工作
    * 5、读取并写入
    * @throws IOException
    * @throws ClientProtocolException
    *
    */

     1 public static String getHttpResult(String path) throws ClientProtocolException, IOException{
     2     /*0、初始化要发送的数据用list存储*/
     3         List<NameValuePair> list=new ArrayList<NameValuePair>();
     4         list.add(new BasicNameValuePair("name", "zhangsan"));
     5         list.add(new BasicNameValuePair("name", "lisi"));
     6         list.add(new BasicNameValuePair("name", "wangwu"));
     7     /*1、通过path设定传送方式*/
     8         
     9         HttpPost post=new HttpPost(path);
    10     /*2、创建客户端*/
    11         HttpClient client=new DefaultHttpClient();
    12         //通过post表单方式发送数据给服务器
    13         
    14         //建立表单
    15         UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,"utf-8");
    16         //装载到post中
    17         post.setEntity(entity);
    18         
    19         HttpResponse response=client.execute(post);
    20     /*3、得到输入流*/
    21         if(response.getStatusLine().getStatusCode()==200){
    22             InputStream in=response.getEntity().getContent();
    23             
    24     /*4、读取流准备工作*/
    25             ByteArrayOutputStream bos=new ByteArrayOutputStream();
    26             byte[]arr=new byte [1024];
    27             int len=0;
    28             
    29     /*5、读取并写入*/
    30             while((len=in.read(arr))!=-1){
    31                 bos.write(arr, 0, len);
    32             }
    33             byte[]b=bos.toByteArray();
    34             return new String(b,0,b.length);
    35         }
    36         
    37         
    38         
    39         return null;
    40     }
    41     
  • 相关阅读:
    类和接口的区别
    ref 和out的区别
    重载 重写 多态区别
    UML种类与类的关系
    Window.open() 全攻略
    C#委托
    RT Diagnostics Routines
    tar.bz2 解压命令。
    教你10招最有效防电脑辐射方法
    预处理,编译, 优化, 汇编, 链接
  • 原文地址:https://www.cnblogs.com/zxxiaoxia/p/4320793.html
Copyright © 2011-2022 走看看