http超文本传输i协议(请求,上传文件,断点续传等)

http协议:超文本传输i协议(介于应用层和传输层协议之间的一个安全协议。)
请求方式 :GET POST{
GET 获取 没有 请求体(向服务器索要数据)
POST 发送 (向服务器提交数据)
}

请求协议:request{
1.请求首行
请求方式 +网址 +协议版本号
2.请求头部信息
客户端告诉服务器的基本信息
3.空行
4.请求体(向服务器提交数据)
}
响应协议:response{
1.响应首行
HTTP/1.1 200 OK
服务端版本号+响应吗(1xx 指示信息 200 请求成功 3xx重新定向 4xx客户端错误 5xx服务器错误)+响应码状态
2.响应头部信息
服务器给客户端的基本信息
3.空行
4.响应体
服务端返回给服务端的数据
}

HTTPS和HTTP的区别是什么
1、HTTPS是加密传输协议,HTTP是名文传输协议;
2、HTTPS需要用到SSL证书,而HTTP不用;
3、HTTPS比HTTP更加安全,对搜索引擎更友好,利于SEO
4、 HTTPS标准端口443,HTTP标准端口80;
5、 HTTPS基于传输层,HTTP基于应用层;

HTTP 请求响应常见状态码
100~199:表示成功接收请求,要求客户端继续提交下一次请求才能完成整个处理过程。
200~299:表示成功接收请求并已完成整个处理过程。常用200
300~399:为完成请求,客户需进一步细化请求。例如:请求的资源已经移动一个新地址、常用302(意味着你请求我,我让你去找别人),307和304(我不给你这个资源,自己拿缓存)
400~499:客户端的请求有错误,常用404(意味着你请求的资源在web服务器中没有)403(服务器拒绝访问,权限不够)
500~599:服务器端出现错误,常用500

HTTP请求的方法:
HTTP/1.1协议中共定义了八种方法(有时也叫“动作”),来表明Request-URL指定的资源不同的操作方式(常用 GET POST)
1、OPTIONS
返回服务器针对特定资源所支持的HTTP请求方法,也可以利用向web服务器发送‘*’的请求来测试服务器的功能性
2、HEAD
向服务器索与GET请求相一致的响应,只不过响应体将不会被返回。这一方法可以再不必传输整个响应内容的情况下,就可以获取包含在响应小消息头中的元信息。
3、GET
向特定的资源发出请求。它本质就是发送一个请求来取得服务器上的某一资源。资源通过一组HTTP头和呈现数据(如HTML文本,或者图片或者视频等)返回给客户端。GET请求中,永远不会包含呈现数据。
4、POST
向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。 Loadrunner中对应POST请求函数:web_submit_data,web_submit_form
5、PUT
向指定资源位置上传其最新内容
6、DELETE
请求服务器删除Request-URL所标识的资源
7、TRACE
回显服务器收到的请求,主要用于测试或诊断
8、CONNECT
HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。

get 和 post区别
get请求无消息体,只能携带少量数据
post请求有消息体,可以携带大量数据
携带数据的方式:
get请求将数据放在url地址中
post请求将数据放在消息体中
GET请求请提交的数据放置在HTTP请求协议头中,而POST提交的数据则放在实体数据中;
GET方式提交的数据最多只能有1024字节,而POST则没有此限制。

网络七层协议:
应用层
示例:TELNET,HTTP,FTP,NFS,SMTP等。

表示层
示例:加密,ASCII等。

会话层
示例:RPC,SQL等。

传输层
示例:TCP,UDP,SPX。

网络层
示例:IP,IPX等。

数据链路层
示例:ATM,FDDI等。

物理层
示例:Rj45,802.3等。

代码 GET 请求

public class MyURL extends Thread {

    private String path;//路径
    private String str_url;//网址
    private Handler handler;//线程

    public MyURL(String path, String str_url, Handler handler) {
        this.path = path;
        this.str_url = str_url;
        this.handler = handler;
    }

    @Override
    public void run() {
        try {
            URL url = new URL(str_url);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.connect();
            if(connection.getResponseCode()==200){
                int max = connection.getContentLength();
                Message message = Message.obtain();
                message.what= Myfinal.max;
                message.obj=max;
                handler.sendMessage(message);
                InputStream is = connection.getInputStream();
                FileOutputStream bos = new FileOutputStream(path);
                int len=0;
                int count=0;
                byte[] bytes = new byte[1024];
                while((len=is.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                    count+=len;
                    Message message1 = Message.obtain();
                    message1.what=Myfinal.count;
                    message1.obj=count;
                    handler.sendMessage(message1);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码 POST 请求

public class MyURL_dl_zc extends Thread {
    private String url;
    private String editText_a;
    private String editText_b;
    private Handler handler;

    public MyURL_dl_zc(String url, String editText_a, String editText_b, Handler handler) {
        this.url = url;
        this.editText_a = editText_a;
        this.editText_b = editText_b;
        this.handler = handler;
    }


    @Override
    public void run(){
        try {
            URL url = new URL(this.url);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            StringBuilder stringBuilder = new StringBuilder();
            String user = editText_a;
            String pass = editText_b;
            connection.getOutputStream().write(("phone+"+user+"passwd"+pass).getBytes());
            connection.connect();
            if(connection.getResponseCode()==200){
                InputStream is = connection.getInputStream();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                int len=0;
                byte[] bytes = new byte[1024];
                while((len=is.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                    stringBuilder.append(bos);
                    Message message = new Message();
                    message.what=Myfinal.post;
                    message.obj=bos;
                    handler.sendMessage(message);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码 断点续传

  private int end;
    private String path;
    private String str_url;
    private Handler handler;
    private int start;

    public MyURL_d(String path, String str_url, Handler handler) {
        this.path = path;
        this.str_url = str_url;
        this.handler = handler;
    }

    @Override
    public void run() {
        try {
            URL url = new URL(str_url);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.connect();
            if(connection.getResponseCode()==200){
                //最大值
                int max = connection.getContentLength();
                Message message = Message.obtain();
                message.what= Myfinal.max_d;
                message.obj=max;
                handler.sendMessage(message);
                //尾
                end=max;
            }
            URL url_d = new URL(str_url);
            File file = new File(path);
            //是否存在
            if(file.exists()){
                start= (int) file.length();
            }
            HttpURLConnection connection_d = (HttpURLConnection) url_d.openConnection();
            connection_d.setRequestMethod("GET");
            connection_d.setConnectTimeout(5000);
            //请求属性(键值对形势)
            connection_d.setRequestProperty("Range","bytes="+start+"-"+end);
            connection_d.connect();
            if(connection_d.getResponseCode()==206){
                InputStream is = connection_d.getInputStream();
                //随机流
                RandomAccessFile rw = new RandomAccessFile(path, "rw");
                //设置从哪开始
                rw.seek(start);
                int len=0;
                int count=0;
                byte[] bytes = new byte[1024];
                while((len=is.read(bytes))!=-1){
                    rw.write(bytes,0,len);
                    count+=len;
                    Log.i("!","count:"+count);
                    Log.i("!","count:"+start);
                    Message message1 = Message.obtain();
                    message1.what=Myfinal.count_d;
                    message1.obj=count;
                    handler.sendMessage(message1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码 上传文件

public class MyURL_sc extends Thread {
    private String a;
    private String path;
    private String file;

    public MyURL_sc(String a, String path, String file) {
        this.a = a;
        this.path = path;
        this.file = file;
    }

    @Override
    public void run(){
        try {
            URL url = new URL(a);
            FileInputStream fileInputStream = new FileInputStream(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //用于字符串的拼接
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.append("-----------------------------7e324741816d4"+"\r\n");
            stringBuilder.append("Content-Disposition: form-data; name=\"file\"; filename=\""+file+"\""+"\r\n");
            stringBuilder.append("Content-Type: image/gif" + "\r\n");
            stringBuilder.append("\r\n");
            byte[] bytes = stringBuilder.toString().getBytes();
            //请求头
            connection.setRequestProperty("Content-Length",bytes.length+new File(path).length()+"");
            connection.setRequestProperty("Content-Type","multipart/form-data; boundary=7e324741816d4");
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            OutputStream outputStream = connection.getOutputStream();
            //请求体
            outputStream.write(bytes);
            int len=0;
            byte[] bytes1 = new byte[1024];
            while((len=fileInputStream.read(bytes1))!=-1){
                outputStream.write(bytes1,0,len);
            }
            connection.connect();
            Log.i("!@#","dasdsadsa:"+connection.getResponseCode());
            if(connection.getResponseCode()==200){
                System.out.println("Ok");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值