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

本文共 2530 字,大约阅读时间需要 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<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
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");
//可以加入多个 FileBody
FileBody 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://vbegi.baihongyu.com/

你可能感兴趣的文章
如何在JavaScript中编写一个简单的Bug跟踪器
查看>>
jQuery 效果 - 滑动
查看>>
对Java多态的深入理解
查看>>
javascript重点-表达式和运算符_优就业
查看>>
springmvc整合poi导出报表
查看>>
Oracle Data Guard延迟的原因
查看>>
java8 遍历数组的几种方式
查看>>
java基础知识(七)--Object类
查看>>
Object.prototype.toString_优就业
查看>>
JS之浏览器对象BOM
查看>>
JAVA面试、进阶必备——堆内存与栈内存
查看>>
springboot(十一):Spring boot中mongodb的使用
查看>>
Java基础之IO流判断文件夹或文件是否存在及其如何创建?
查看>>
java系列(八)枯燥的基础总结
查看>>
北漂面试经历(一(两)年工作经验)——Java基础部分
查看>>
一道有关内存泄漏的阿里巴巴JAVA工程师笔试题
查看>>
多线程之synchronized锁字符串对象的一个易错点
查看>>
JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
查看>>
Java快速排序
查看>>
Linux系统基础-基本系统管理命令
查看>>