GET在浏览器回退时是无害的,而POST会再次提交请求。
GET产生的URL地址可以被Bookmark,而POST不可以。
GET请求会被浏览器主动cache,而POST不会,除非手动设置。
GET请求只能进行url编码,而POST支持多种编码方式。
GET请求参数会被完整保留在浏览器历史记录里,而POST中的参数不会被保留。
GET请求在URL中传送的参数是有长度限制的,而POST么有。
对参数的数据类型,GET只接受ASCII字符,而POST没有限制。
GET比POST更不安全,因为参数直接暴露在URL上,所以不能用来传递敏感信息。
GET参数通过URL传递,POST放在Request body中。
(本标准答案参考自w3schools)
Http是什么 get和post是什么
HTTP的底层是TCP/IP。所以GET和POST的底层也是TCP/IP,也就是说,GET/POST都是TCP链接。GET和POST能做的事情是一样一样的。你要给GET加上request body,给POST带上url参数,技术上是完全行的通的。 | |
---|
| |
来看代码
package com.example.study_thirdween;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
ImageView imageView;
HandlerThread handlerThread;
Handler handler;
private final int DOWNLOAD=1;
private final int REGISTER=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
imageView=findViewById(R.id.image);
findViewById(R.id.Register).setOnClickListener(this);
findViewById(R.id.downLoadFile).setOnClickListener(this);
handlerThread=new HandlerThread("handlerThread");//参数随便写
handlerThread.start();
handler=new HttpHandler(handlerThread.getLooper());//让handler
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.downLoadFile:
handler.sendEmptyMessage(DOWNLOAD);
break;
case R.id.Register:
handler.sendEmptyMessage(REGISTER);
break;
}
}
private void Register() {
String Registerurl="http://169.254.230.253:8080/register";
try {
URL url=new URL(Registerurl);
HttpURLConnection urlC =(HttpURLConnection) url.openConnection();
urlC.setRequestMethod("POST");
urlC.setConnectTimeout(1000*5);
urlC.setReadTimeout(1000*5);
urlC.setDoInput(true);//允许读取数据
urlC.setDoOutput(true);//允许写入 form
urlC.setRequestProperty("Content-type","application/x-www-form-urlencoded");//以表单形式传递参数
String postParms="name=兵神&password=admin&verifyCode=666";
OutputStream os=urlC.getOutputStream();
os.write(postParms.getBytes());//把参数发送进去
os.flush();
StringBuffer buffer=new StringBuffer();
if(urlC.getResponseCode()==200){
InputStream is = urlC.getInputStream();
BufferedInputStream bis=new BufferedInputStream(is);
byte[] b=new byte[1024];
int len=-1;
while ((len=bis.read(b))!=-1){
buffer.append(new String(b,0,len));
}
Log.e("##",buffer.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//使用handlerThread
private void DonwloadFile() {
String downLoadUrl="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=734860758,1526830543&fm=15&gp=0.jpg";
String savePath="/sdcard/coolboy.jpg";
File file = new File(savePath);
if(file.exists()){
file.delete();
}
FileOutputStream fos=null;
BufferedInputStream bis=null;
try {
URL url=new URL(downLoadUrl);
HttpURLConnection urlC=(HttpURLConnection) url.openConnection();
urlC.setRequestMethod("GET");
urlC.setConnectTimeout(1000*5);
urlC.setReadTimeout(1000*5);
urlC.setDoInput(true);
urlC.connect();
//400客户端错误 500服务端错误
if(urlC.getResponseCode()==200){
InputStream is =urlC.getInputStream();
bis=new BufferedInputStream(is);//文件输入流
fos=new FileOutputStream(savePath);
byte[] b=new byte[1024];
int len=-1;
while ((len=bis.read(b))!=-1){
fos.write(b,0,len);//写入文件
}
fos.flush();//强制把数据写入磁盘
final Bitmap bitmap= BitmapFactory.decodeFile(savePath);
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
bis.close();
fos.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bis!=null)
bis.close();
if(fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class HttpHandler extends Handler{
public HttpHandler(Looper looper){
super(looper);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case DOWNLOAD:
//get请求图片
DonwloadFile();
break;
case REGISTER:
//post请求服务器
Register();
break;
}
}
}
}
Get请求网络图片效果:
