博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络_httpClient
阅读量:4290 次
发布时间:2019-05-27

本文共 2489 字,大约阅读时间需要 8 分钟。

apache的包,用着很爽。厚实的基础库。
// HttpClient
get请求
DefaultHttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet("http://192.168.1.29:8080/itheima83/servlet/LoginServlet?username=abcdef&pwd=123456");BasicHttpResponse response = (BasicHttpResponse) client.execute(get);int status = response.getStatusLine().getStatusCode();if(status == 200){	InputStream is = response.getEntity().getContent();	ByteArrayOutputStream bos = new ByteArrayOutputStream();	byte[] buffer = new byte[1024*10];	int len =0;	while((len = is.read(buffer)) != -1){		bos.write(buffer,0,len);	}	message = bos.toString();	bos.close();	is.close();}
// HttpClient
post请求
DefaultHttpClient client = new DefaultHttpClient();//HttpGet get = new HttpGet("http://192.168.1.29:8080/itheima83/servlet/LoginServlet?username=abcdef&pwd=123456");HttpPost post = new HttpPost("http://192.168.1.29:8080/itheima83/servlet/LoginServlet");//设置请求体List
list = new ArrayList
();BasicNameValuePair value1 = new BasicNameValuePair("username","abcdef");BasicNameValuePair value2 = new BasicNameValuePair("pwd","123456");list.add(value1);list.add(value2);UrlEncodedFormEntity myentity;myentity = new UrlEncodedFormEntity(list);post.setEntity(myentity);BasicHttpResponse response = (BasicHttpResponse) client.execute(post);int status = response.getStatusLine().getStatusCode();if(status == 200){ InputStream is = response.getEntity().getContent(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024*10]; int len =0; while((len = is.read(buffer)) != -1){ bos.write(buffer,0,len); } message = bos.toString(); bos.close(); is.close();}
// post 文件上传(这个库,下载容易,上传难。因为请求体的封装类好像没有)
//android 自带的 apche库。没有最新的上传请求体包装类。
MultipartEntity ;
需要加入新版本的HttpClient的库jar,才能使用组件上传
// 各个版本,支持上传的类相当不同. 在jar 库里有。android 自带的。
测试 HttpClient4.1.3,不需要依赖库。最新到4.5.1。
HttpPost post = new HttpPost("http://192.168.1.29:8080/mytry/Upload");File file = new File("/data/data/com.hunty.upanddown/files/wo.java");//可以加入多个 FileBodyFileBody body = new FileBody(file);MultipartEntity myentity = new MultipartEntity();myentity.addPart("zhangjianqiu.png", body);post.setEntity(myentity);
// post 下载
InputStream is = response.getEntity().getContent();FileOutputStream bos = new FileOutputStream("/data/data/com.hunty.upanddown/files/zhangjianqiu.png");byte[] buffer = new byte[1024*10];int len =0;while((len = is.read(buffer)) != -1){	bos.write(buffer,0,len);}bos.close();
获得的实体,转型封装成别的对象 FileEntity或者InputStreamEntity失败。

转载地址:http://wdegi.baihongyu.com/

你可能感兴趣的文章
几种常用JSON库性能比较,为什么fastjson那么快
查看>>
再有人问你分布式锁,就把这个丢给他!
查看>>
Mysql优化小技巧
查看>>
Nginx作为缓存服务
查看>>
了解 Nginx 的基本概念
查看>>
count(*),count(1)和count(字段)的区别
查看>>
面试大杀器:为什么一定要用MQ中间件?
查看>>
漫谈 MySQL 的锁机制
查看>>
Java 中 long 和 double 的原子性?
查看>>
Nginx和Apache的对比
查看>>
一文带你看懂Spring事务!
查看>>
Spring Boot实战:拦截器与过滤器
查看>>
夯实基础:关于线程的Callable和Future两个类解析
查看>>
常用的设计模式汇总,超详细!
查看>>
一篇文章了解RPC框架原理
查看>>
技术进阶:ThreadPoolExecutor源码解析之机制原理篇
查看>>
nginx搭建简易负载均衡服务
查看>>
SpringCloud之Feign(Finchley版)
查看>>
应用框架:Spring IOC基于注解启动的分析
查看>>
基于注解@Aspect的AOP实现
查看>>