Android 网络编程(三)HttpURLConnection

版权声明:本文出自朋永的博客,转载必须注明出处。 https://blog.csdn.net/VNanyesheshou/article/details/76423401

HttpURLConnection
HTTP的URLConnection(RFC 2616)用于通过Web发送和接收数据。数据可以是任何类型和长度。该类可用于发送和接收长度未预先知道的流数据。


使用步骤

HttpURLConnection使用步骤:

  1. 通过调用URL.openConnection()获取新的HttpURLConnection并将结果转换为HttpURLConnection。
  2. 准备请求
    请求的主要属性是其URI。请求标头还可以包括诸如证书,content types和session cookies。
  3. 可选地上传请求正文。如果它们包含请求体,必须使用setDoOutput(true)来配置实例。通过写入getOutputStream()返回的流来发送数据。
  4. 读响应。
    响应头通常包括元数据,如响应主体的内容类型和长度,修改的日期和会话cookie。响应主体可以从getInputStream()返回的流中读取。如果响应没有body,那么该方法返回一个空的流。
  5. 断开。
    一旦响应正文被读取,HttpURLConnection应该通过调用disconnect()关闭。断开连接释放连接所持有的资源,以使其可能被关闭或重新使用。

Get请求

public static String getTask(String path){
    try {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();           
        conn.setRequestMethod("GET");  //设置请求方式
        conn.setConnectTimeout(5*1000); //设置连接超时时间
        conn.setRequestProperty("Connection", "Keep-Alive");
        InputStream in = conn.getInputStream();
        //BufferedInputStream带有缓冲区的输入流
        BufferedInputStream bis = new BufferedInputStream(in);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[]array = new byte[1024];
        int len =0;
        //读取数据
        while((len = bis.read(array)) != -1){
            bos.write(array, 0, len);
        }
        bis.close();
        bos.close();
        conn.disconnect();  //断开连接
        String result = bos.toString("utf-8");
        Log.i(TAG, result);
        return result;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

打印

08-25 00:12:27.254: I/MainActivity(1140): <!DOCTYPE html>
08-25 00:12:27.254: I/MainActivity(1140): <html>
08-25 00:12:27.254: I/MainActivity(1140):     <!--STATUS OK-->
08-25 00:12:27.254: I/MainActivity(1140):     <head>
08-25 00:12:27.254: I/MainActivity(1140):         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
08-25 00:12:27.254: I/MainActivity(1140):         <title>百度一下,你就知道</title>
08-25 00:12:27.254: I/MainActivity(1140):         <meta http-equiv="Cache-control" content="no-cache">
//.....

setRequestMethod函数参数为String类型,必须为以下几种:OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE,必须是大写。
HttpURLConnection默认情况下使用GET请求。如果setDoOutput(true)已经被调用,它将使用POST。其他HTTP方法(OPTIONS,HEAD,PUT,DELETE和TRACE)可以使用setRequestMethod(String)设置。


Post请求

代码如下:

public static String postTask(String path,String post){
    URL url = null;
    try {
        url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST"); // 提交模式
        conn.setConnectTimeout(10000);//连接超时 单位毫秒
        conn.setReadTimeout(2000);//读取超时 单位毫秒
        conn.setDoOutput(true);  // 发送POST请求必须设置为true
        conn.setDoInput(true);   //默认为true。
        conn.setFixedLengthStreamingMode(post.length());
        // 获取URLConnection对象对应的输出流
        PrintWriter printWriter = new PrintWriter(conn.getOutputStream());
        // 发送请求参数
        printWriter.write(post);  //post的参数 x=x&y=y
        // flush输出流的缓冲
        printWriter.flush();
        //开始获取数据
        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int len;
        byte[] arr = new byte[1024];
        while((len=bis.read(arr))!= -1){
            bos.write(arr,0,len);
            bos.flush();
        }
        bis.close();
        bos.close();
        conn.disconnect();  //断开连接
        Log.i(TAG, bos.toString("utf-8"));
        return bos.toString("utf-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

打印如下:

08-25 02:25:55.144: I/MainActivity(1205): {"success":"登录成功"}

要将数据上传到Web服务器、使用Post请求,请使用配置连接进行输出 setDoOutput(true)。
为了获得最佳性能,应该setFixedLengthStreamingMode(int)提前知道身体长度,或者setChunkedStreamingMode(int)不知道身长。否则HttpURLConnection将被强制缓冲内存中的完整请求体,然后再传输,浪费(可能耗尽)堆并增加延迟。
当body长度已事先知道时,应该调用setFixedLengthStreamingMode(int),或者不知道长度时调用setChunkedStreamingMode(int)。 否则HttpURLConnection将被强制缓存内存中的完整请求体,然后再传输,浪费(可能耗尽)堆并增加延迟。


性能

HttpURLConnection,此类返回的输入和输出流不会缓冲。大多数调用者都应该使用BufferedInputStream或BufferedOutputStream来包装返回的流。仅进行批量读取或写入的调用者可以省略缓冲。
当向服务器传送大量数据或从服务器传输大量数据时,请使用流来限制内存中的数据量。除非您需要将整个数据一次存入内存,否则将其作为流处理(而不是将完整的数据存储为单字节数组或字符串)。

为了减少延迟,该类可以为多个请求/响应对重用相同的底层Socket。 因此,HTTP连接可能会持续时间超过必要时间。 对disconnect()的调用可能会将套接字返回到连接的套接字池。 可以通过在发出任何HTTP请求之前将http.keepAlive系统属性设置为false来禁用此行为。 http.maxConnections属性可用于控制每个服务器将持有多少空闲连接。

默认情况下,HttpURLConnection请求服务器使用gzip压缩。由于getContentLength()返回传输的字节数,因此您无法使用该方法预测从getInputStream()可以读取多少字节。而是读取该流,直到它:read()返回-1。可以通过在请求标头中设置可接受的编码来禁用Gzip压缩:

urlConnection.setRequestProperty("Accept-Encoding", "identity");

网络登录

一些Wi-Fi网络阻止Internet访问,直到用户点击登录页面。 这种登录页面通常使用HTTP重定向来呈现。 您可以使用getURL()来测试您的连接是否意外重定向。 此检查无效,直到收到响应标头,您可以通过调用getHeaderFields()或getInputStream()触发。 例如,要检查响应是否未重定向到意外的主机:

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try {
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    if (!url.getHost().equals(urlConnection.getURL().getHost())) {
      // we were redirected! Kick the user out to the browser to sign on?

    ...
    } finally {
        urlConnection.disconnect();
    }
}

欢迎大家关注、评论、点赞
你们的支持是我坚持的动力。

猜你喜欢

转载自blog.csdn.net/VNanyesheshou/article/details/76423401