Http基本使用方法
一:HTTP传输协议
1.什么是HTTP协议(概念)
HTTP,超文本传输协议,英文全称是Hypertext Transfer Protocol,它是互联网上应用最为广泛的一种网络协议。HTTP是一种应用层协议,它是基于TCP协议之上的请求/响应式的协议,即一个客户端与服务器建立连接后,向服务器发送一个请求;服务器接到请求后,给予相应的响应信息。
2.HTTP1.0和HTTP1.1的区别
http1.0是非持续连接
http1.1是长久持续连接
二:GET与POST请求
1.八种请求方式,其中最常用的是GET和POST
1、OPTIONS
返回服务器针对特定资源所支持的HTTP请求方法,也可以利用向web服务器发送‘*’的请求来测试服务器的功能性
2、HEAD
向服务器索与GET请求相一致的响应,只不过响应体将不会被返回。这一方法可以再不必传输整个响应内容的情况下,就可以获取包含在响应小消息头中的元信息。
3、GET
向特定的资源发出请求。它本质就是发送一个请求来取得服务器上的某一资源。资源通过一组HTTP头和呈现数据(如HTML文本,或者图片或者视频等)返回给客户端。
4、POST
向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。
5、PUT
向指定资源位置上传其最新内容
6、DELETE
请求服务器删除Request-URL所标识的资源
7、TRACE
回显服务器收到的请求,主要用于测试或诊断
8、CONNECT
HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。
2.get请求和post请求的区别
get请求直接将请求参数暴露在url,不安全+一般用于向服务器请求数据
post请求将请求参数放在请求体里面,安全的+一般用于向服务器提交数据
3.请求协议和响应协议
1.请求协议:
请求首行:
请求头信息:客户端告诉服务器我这边的信息
空行
请求体:get请求是没有请求体的
2.响应协议:
响应首行:HTTP/1.1 200 OK
响应头信息:Content-Length服务器返回数据的总大小
空行
响应体:服务器返回的数据
4.响应码
200:OK,成功
400:服务器无法识别
500:服务器内部错误
三:文件上传
四:文件下载
public static void download(String url,String path) {
FileOutputStream fileOutputStream = null;
InputStream inputStream=null;
try {
URL url1 = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
if(urlConnection.getResponseCode()==200){
inputStream = urlConnection.getInputStream();
fileOutputStream = new FileOutputStream(path);
//边读边写
byte[] bytes=new byte[1024];
int len=0;
while((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {//关流
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.apk下载以及断点续传
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app4">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
代码
public class MainActivity extends AppCompatActivity {
private ProgressBar bar;
private Button startId,endId;
private String path = "http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4";
int endnum = 0;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 101){
//读完了
Toast.makeText(MainActivity.this, "下载完毕", Toast.LENGTH_SHORT).show();
}else if (msg.what == 102){
bar.setMax(endnum);
}else if (msg.what == 103){
bar.setProgress(msg.arg1);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startId = findViewById(R.id.start_id);
endId = findViewById(R.id.pause_id);
bar = findViewById(R.id.pro_id);
startId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyThread().start();
}
});
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
getLength(path);
downLoad(path);
}
}
public void getLength(String url){
URL url1 = null;
try {
url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
if (connection.getResponseCode() == 200){
endnum = connection.getContentLength();//文件最大值
//是为了给进度条来设置
Message message = new Message();
message.what = 102;
message.obj = endnum;
}
} catch (Exception e) {
e.printStackTrace();
}
}
//断点下载,更新进度,分批保存,是否是否完成
public void downLoad(String url){
int start = 0;
int end = 1024;
int sum = 0;//累计长度
try {
URL url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Range","bytes = "+start+"-"+end);//下载范围0-1024
if (connection.getResponseCode() == 206){//断点下载的ok响应码是206
InputStream inputStream = connection.getInputStream();
//读后写入文件
String filepath ="/sdcard/a1705.mp4";
RandomAccessFile file = new RandomAccessFile(filepath,"rw");
file.seek(start); //设置从哪开始写
int len = 0;
byte[] bys = new byte[1024];
while ((len = inputStream.read(bys))!=-1){
file.write(bys,0,len); //写文件内容
sum+=len;//记录是否完成更新进度
Thread.sleep(1000);
Message message = new Message();
message.what = 103;
message.arg1 = sum;
handler.sendMessage(message);//更新进度值
if (sum == end){
handler.sendEmptyMessage(101);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}