java发送http的get和post请求

Posted 菲菲菲菲菲常新的新手

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
}

 

以上是关于java发送http的get和post请求的主要内容,如果未能解决你的问题,请参考以下文章

用java做一个httpClient 发送https 的get请求,需要证书验证的那种,求大神指点一下!

Java后台模拟发送http的get和post请求,并测试

求教golang中http发送post请求gb2312编码的解决方案

三个例子 —JAVA发送http get/post请求,调用http接口方法

Java Socket 实现HTTP与HTTPS协议发送POST/GET请求

Java发送http get/post请求,调用接口/方法