码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-03-07 19:54:23      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

实现该方法需要导入一些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     

 

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

标签:

原文地址:http://www.cnblogs.com/zxxiaoxia/p/4320793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!