• HttpURLConnection类的使用


    此类以获取天气的一个api地址为例:

    package javaexcjs;
    
    import java.io.BufferedReader;
    import java.io.OutputStreamWriter;
    
    import java.io.InputStream;
    
    import java.io.InputStreamReader;
    
    import java.net.HttpURLConnection;
    import java.net.URLEncoder;
    
    import java.net.URL;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    public class CopyOfSendPostRequest {
        static String sessionId = "";
    
        public static void main(String[] args) throws Exception {
            
            //城市名称
            String city = URLEncoder.encode("重庆", "GB2312");
            System.out.println(city);
            // api url :北向URL
            String locationUrl = "http://php.weather.sina.com.cn/xml.php?city=" + city + "&password=DJOYnieT8234jlsK&day=0";
    
            // http body 消息体
            String reqBody = "";
    
            // http method
            String method = "POST";
    
            // http head : Content-Type 消息类型
            String contentType = "application/json;charset=UTF-8";
    
            // 设定连接的相关参数
            URL url = new URL(locationUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setReadTimeout(10000);
            connection.setRequestMethod(method);
            connection.setRequestProperty("Content-Type", contentType);
    
            //写入请求消息体
            OutputStreamWriter out = new OutputStreamWriter(
                    connection.getOutputStream(), "UTF-8");
            out.write(reqBody);
            out.flush();
            out.close();
    
            // 获取服务端的反馈
            String strLine = "";
            StringBuilder strResponse = new StringBuilder();
            try {
                Map<String, List<String>> rspHeaders = connection.getHeaderFields();
    
                Set<String> rspHeadNames = rspHeaders.keySet();
                for (String key : rspHeadNames) {
                    //rspHeaders中的http状态码和描述的键为null
                    if (null != key) {
                        strResponse.append(key + ": ");
                    }
                    strResponse.append(new String(rspHeaders.get(key).get(0)
                            .getBytes("iso-8859-1"), "UTF-8")
                            + "
    ");
                }
    
                int code = connection.getResponseCode();
                // String status = connection.getResponseMessage();
                InputStream in;
    
                // 判断http状态码
                if (code == 200) {
                    in = connection.getInputStream();
                } else {
                    in = connection.getErrorStream();
                }
                if (null != in) {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(in));
    
                    while ((strLine = reader.readLine()) != null) {
                        strResponse.append("
    " + strLine);
                    }
                }
                System.out.print(strResponse.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    View Code
  • 相关阅读:
    MySQL-Linux升级MySQL
    查看linux 版本
    mysql 密码找回方法
    CentOS7.6利用systemctl添加自定义系统服务
    centos7.6下定时监测MySQL进程终止后自动重启的方法
    Linux实操篇-Linux磁盘分区、挂载
    阿里云centos7.6下MongoDB安装和配置
    Linux中文件权限 chmod、u+x、u、r、w、x分别代表什么
    ABP 发布以后nlog4.NET写入不到日志文件里
    Android studio gradle 下载很缓慢的解决方法,gradle版本不对
  • 原文地址:https://www.cnblogs.com/fengdeng/p/5807212.html
Copyright © 2020-2023  润新知