zoukankan      html  css  js  c++  java
  • java发送http的get和post请求

    import java.io.*;
    import java.net.URL;
    import java.util.Map;
    import java.net.HttpURLConnection;;
    
    
    public class HttpRequest{
        
        public static String sendGet(String url) {
            String result = "";
            BufferedReader in = null;
            try {
                URL realurl = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) realurl.openConnection();
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("GET");
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }    
            } catch(Exception e) {
                System.out.println("发送get请求失败:"+e);
                e.printStackTrace();
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return result;
        }
        
        public static String sendPost(String url, String params) {
            String encoding = "UTF-8";
            String result = "";
            BufferedReader in = null;
            try {
                byte[] data = params.getBytes(encoding);
                URL base_url = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) base_url.openConnection();
                conn.setReadTimeout(5000);
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Content-Length", String.valueOf(data.length));
                OutputStream outStream = conn.getOutputStream();
                outStream.write(data);
                outStream.flush();
                outStream.close();
                System.out.println(conn.getResponseCode());
                System.out.println(conn.getResponseMessage());
                if (conn.getResponseCode() == 200) {
                    in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String line;
                    while ((line = in.readLine()) != null) {
                        result += line;
                    }
                }
            } catch(Exception e) {
                System.out.println("发送POST请求失败:"+e);
                e.printStackTrace();
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return result;
        }
    }
  • 相关阅读:
    0.0
    《用户故事与敏捷方法》 笔记
    Linux下运行Jmeter压测
    64位Win7系统安装Mysql 5.7.22图文教程
    Zabbix-(七)分布式监控
    Zabbix-(六) JMX监控
    Zabbix-(五)监控Docker容器与自定义jvm监控项
    Zabbix-(四)邮件、钉钉告警通知
    Zabbix-(三)监控主机CPU、磁盘、内存并创建监控图形
    Zabbix-(二) 使用docker部署
  • 原文地址:https://www.cnblogs.com/lgh344902118/p/8529844.html
Copyright © 2011-2022 走看看