前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java Http消息传递之POST和GET两种方法--通过实用工具类来获取服务器资源

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

作者头像
用户3030674
发布2018-09-14 14:59:02
9430
发布2018-09-14 14:59:02
举报

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

可以去一下地址下载:

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

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

代码语言:javascript
复制
 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 * */

代码语言:javascript
复制
 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     
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-03-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档