5.post上传和压缩、插件模拟请求
gzip
gzip一种压缩方式,或者是文件形式,它主要用于网络传输数据的压缩
gzip压缩好不好用
- 浏览器:网速一定.内容越小.请求响应的速度是不是更快
- 手机server:返回数据类型是json/xml->文本-->压缩率会很高.
gzip执行流程
//1. 告知服务器.客户端支持gzip解压
* get.addHeader("Accept-Encoding", "gzip");//2. 根据响应头得知服务器知否进行了gzip压缩
* Header[] headers = response.getHeaders("Content-Encoding"); for (Header header : headers) { if ("gzip".equals(header.getValue())) { isUseGzip = true; } }//3.根据是否使用gzip.解压具体的数据
if (isUseGzip) { GZIPInputStream in = new GZIPInputStream(entity.getContent()); result = IoUtils.convertStreamToString(in); } else { result = EntityUtils.toString(entity); }服务端具体怎么支持gzip压缩.根据使用的服务器语言来定
post请求参数的形式
key-value--->表单:结构比较单一.而且比较繁琐
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); for (String key : parmas.keySet()) { BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, parmas.get(key)); parameters.add(basicNameValuePair); } UrlEncodedFormEntity form = new UrlEncodedFormEntity(parameters); post.setEntity(form);jsonString形式:结构灵活.实际开发用的很多
post.setEntity(new StringEntity(jsonString));file形式:需要jar包:
很多框架上传图片就是用他MultipartEntity entity = new MultipartEntity(); entity.addPart("actimg", new FileBody(file)); post.setEntity(entity);- 多张图片上传:写个循环,用键值,可以用BasicNameValuePair,也可以用file,循环添加,也可以用base64变成string上传

- Base64:byte[]-->string
- 上传图片,语音:
- 存对象到sp:如map
iv = (ImageView) findViewById(R.id.iv);
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//1.bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//2. bitmap-->byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArray = baos.toByteArray();
//3.byte[]->string,这样就可以用string传给服务器
String bitmapString = Base64.encodeToString(byteArray, Base64.DEFAULT);
System.out.println("bitmapString :" + bitmapString);
//4. string-->byte[];
byte[] bitmapByteArr = Base64.decode(bitmapString, Base64.DEFAULT);
//5.byte[]-->bitmap
Bitmap bitmap2 = BitmapFactory.decodeByteArray(bitmapByteArr, 0, bitmapByteArr.length);
//6.在imageView上设置图片
iv.setImageBitmap(bitmap2);
//obj-->byte[] -->string->sp
}
});
post的content-type
- application/x-www-form-urlencoded :表单类型-->key-value,默认
- multipart/form-data :文件上传
- application/json :jsonString
- text/xml :xml
- text/plain:文本
- post.addHeader("Content-Type", "application/json");//如果后台固定死了.不加请求头.同样返回json那就没问题.但是如果后台逻辑和这个请求头有关系.就必须要加上.可以肯定的说.加了万无一失
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGzip();
Map<String, String> map = new HashMap<String, String>();
map.put("name", "billy");
initPostKeyValue(map);
String jsonString = "{\"name\":\"billy\"}";
initPostJsonString(jsonString);
File file = new File("");
initPostFile(file);
}
/**
* 文件的上传
* @param file
*/
private void initPostFile(File file) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("");
//2. 传递二进制类型的参数
MultipartEntity entity = new MultipartEntity();
entity.addPart("actimg", new FileBody(file));
post.setEntity(entity);
httpClient.execute(post);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 使用jsonString post数据库
* @param jsonString
*/
private void initPostJsonString(String jsonString) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("");
post.addHeader("Content-Type", "application/json");
//Content-Type text/xml-->xml
//Content-Type application/json-->json
//2. 传递jsonString类型的参数
post.setEntity(new StringEntity(jsonString));
httpClient.execute(post);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 使用key-value的形式post数据
* @param parmas 请求参数对于的map集合
*/
private void initPostKeyValue(Map<String, String> parmas) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("");
//1. 传递key-value的参数
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
for (String key : parmas.keySet()) {
BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, parmas.get(key));
parameters.add(basicNameValuePair);
}
UrlEncodedFormEntity form = new UrlEncodedFormEntity(parameters);
post.setEntity(form);
httpClient.execute(post);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void initGzip() {
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
boolean isUseGzip = false;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("http://httpbin.org/gzip");
//1. 告知服务器.客户端支持gzip解压
get.addHeader("Accept-Encoding", "gzip");
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
//2. 根据响应头得知服务器知否进行了gzip压缩
Header[] headers = response.getHeaders("Content-Encoding");
for (Header header : headers) {
if ("gzip".equals(header.getValue())) {
isUseGzip = true;
}
}
HttpEntity entity = response.getEntity();
//3.根据是否使用gzip.解压具体的数据
String result = "";
if (isUseGzip) {
GZIPInputStream in = new GZIPInputStream(entity.getContent());
result = IoUtils.convertStreamToString(in);
} else {
result = EntityUtils.toString(entity);
}
System.out.println("result:" + result);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
}
restclient(firfox插件),postman(谷歌浏览器)
网络请求模拟插件,post用,get直接在浏览器网页就打开了
5.post上传和压缩、插件模拟请求的更多相关文章
- 利用canvas对上传图片进行上传前压缩
利用谷歌调式工具发现,图片大小直接影响着首屏加载时间. 且考虑到后期服务端压力,图片压缩特别必要. 本文是前端利用canvas实现图片.参考文章:https://www.cnblogs.com/007 ...
- bootstrap-fileinput上传文件的插件使用总结----编辑已成功上传过的图片
http://plugins.krajee.com/file-plugin-methods-demo 具体操作 http://plugins.krajee.com/file-preview-manag ...
- PHP实现图片上传并压缩
本文实例讲解了PHP图片上传并压缩的实现方法,分享给大家供大家参考,具体内容如下 使用到三个文件 connect.php:连接数据库 test_upload.php:执行SQL语句 upload_im ...
- Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】
本文实例讲述了Java实现的上传并压缩图片功能.分享给大家供大家参考,具体如下: 先看效果: 原图:1.33M 处理后:27.4kb 关键代码: package codeGenerate.util; ...
- Nodejs实现图片的上传、压缩预览、定时删除
前言 我们程序员日常都会用到图片压缩,面对这么常用的功能,肯定要尝试实现一番.第一步,node基本配置 这里我们用到的是koa框架,它可是继express框架之后又一个更富有表现力.更健壮的web框架 ...
- 百度编辑器上传大视频报http请求错误怎么办
百度编辑器UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码,所以受到很多开放人员的青睐.但是有时 ...
- element-ui上传组件,通过自定义请求上传文件
记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...
- C# 应用 - 使用 HttpClient 发起上传文件、下载文件请求
1. 示例代码 using System; using System.IO; using System.Net.Http; /// <summary> /// 下载文件 /// </ ...
- post上传和压缩、插件模拟请求
gzip gzip一种压缩方式,或者是文件形式,它主要用于网络传输数据的压缩 gzip压缩好不好用 浏览器:网速一定.内容越小.请求响应的速度是不是更快 手机server:返回数据类型是json/ ...
随机推荐
- centos6.4yum搭建lamp环境
1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport 8 ...
- JQ系列:css操作
JQ中的 .css()有三种使用方法: $('#p').css('width'); 取得ID为p的样式属性的width值;等同 return width $('#p').css('width','10 ...
- 修改Flume-NG的hdfs sink解析时间戳源码大幅提高写入性能
Flume-NG中的hdfs sink的路径名(对应参数"hdfs.path",不允许为空)以及文件前缀(对应参数"hdfs.filePrefix")支持正则解 ...
- hdu.5203.Rikka with wood sticks(数学推导:一条长度为L的线段经分割后可以构成几种三角形)
Rikka with wood sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- cf.295.C.DNA Alignment(数学推导)
DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Spring常用的接口和类(三)
一.CustomEditorConfigurer类 CustomEditorConfigurer可以读取实现java.beans.PropertyEditor接口的类,将字符串转为指定的类型.更方便的 ...
- CSS包含块containing block详解
“包含块(containing block)”,W3c中一个很重要的概念,今天带大家一起来好好研究下. 初步理解 在 CSS2.1 中,很多框的定位和尺寸的计算,都取决于一个矩形的边界,这个矩形,被称 ...
- UIWebView
本地html string文件 loadHTMLString: 本地/远程文件 loadRequest
- Linux system V
Sysvinit 的小结 Sysvinit 的优点是概念简单.Service 开发人员只需要编写启动和停止脚本,概念非常清楚:将 service 添加/删除到某个 runlevel 时,只需要执行一些 ...
- Selenium测试Ajax程序(转)
上周末参加了Qclub的百度技术沙龙,听了百度的孙景卫讲了Web自动化测试,讲的非常好,然后在小组讨论时又有幸座在了一起.我们讨论的一个内容,就是Ajax应用程序比原来的非Ajax程序更不易测试,这里 ...
很多框架上传图片就是用他