zoukankan      html  css  js  c++  java
  • HttpURLConnection和HttpClient使用

    HttpURLConnection

    这是Java的标准类,继承自URLConnection,可用于向指定网站发送GET/POST请求。

    方法描述

    void setRequestMethod(String method)
    设置请求的方法,注意参数必须全大写;

    void setFollowRedirects(boolean set)
    设置是否自动重定向,默认是true;

    void setRequestProperty(String key, String value)
    设置请求的header;

    使用步骤

    第一步 使用java.net.URL封装HTTP资源的url,并使用openConnection方法获得HttpURLConnection对象;
    第二步 设置请求方法,setRequestMethod("POST");
    第三步 设置输入输出和其它权限;
    第四步 设置http请求头,setRequestProperty("Charset", 'UTF-8");
    第五步 输入和输出数据;对InputStream和OutputStream的读写操作。
    第六步 关闭输入和输出流。

    示例代码如下:

        private void sendPost() {
            HttpURLConnection connection = null;
            BufferedWriter writer = null;
            try {
                URL url = new URL("http", "www.baidu.com", 80, "/");
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(false);
                connection.setDoInput(true);
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("Charset", "UTF-8");
                OutputStream os = connection.getOutputStream();
                writer = new BufferedWriter(new OutputStreamWriter(os));
                writer.write("user=chen&pass=ya");
                writer.flush();
                connection.connect();
    
                if (connection.getResponseCode() == 200) {
                      recv(connection.getInputStream());
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (writer != null) {
                        writer.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                if (connection != null) {
                    connection.disconnect();
                }
            }
        }
    
        private void recv(@NonNull InputStream is) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            try {
                String content;
                while ((content = reader.readLine()) != null) {
                    System.out.println("recv: " + content);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    

    HttpClient

    是Apache包的一个Http请求库,封装了很多功能(如:cookie管理),Android 5.0开始已经将此库移除了sdk。



    作者:Charein
    链接:https://www.jianshu.com/p/93518011e4c3
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    C++基于范围的for循环性能测试(针对std::vector)
    C++ 中std::function 、std::bind的使用和lambda的使用
    C++ auto 关键字的使用
    C++内存管理解析
    c++类内存分布解析
    Windows上编译GRPC
    在从1到n的正数中1出现的次数
    POJ 1009 解题报告
    Cheat Engine 笔记
    Cheat Engine 教程 Step 9
  • 原文地址:https://www.cnblogs.com/xuqiang7/p/11773830.html
Copyright © 2011-2022 走看看