Java教程

JAVA实现GET和POST请求

本文主要是介绍JAVA实现GET和POST请求,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、有关GET和POST请求的注释

1.1 有关 GET 请求的其他一些注释:

  • GET 请求可被缓存
  • GET 请求保留在浏览器历史记录中
  • GET 请求可被收藏为书签
  • GET 请求不应在处理敏感数据时使用
  • GET 请求有长度限制
  • GET 请求只应当用于取回数据(不修改)
    示例:百度api一般是get请求,除了请求地址,还有参数,?分割地址和参数:
    https://api.map.baidu.com/geocoding/v3/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation

1.2 有关 POST 请求的其他一些注释:

  • POST 请求不会被缓存
  • POST 请求不会保留在浏览器历史记录中
  • POST 不能被收藏为书签
  • POST 请求对数据长度没有要求

二、JAVA实现GET和POST请求

2.1 GET请求

//Get请求
public static String loadGET(String url,String param){
    StringBuilder json = new StringBuilder();
    BufferedReader in = null;
    try {
        String getUrl = url + "?" + param;
        URL requestUrl = new URL(getUrl);
        //打开和URL之间的连接
        HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
        //请求方式
        connection.setRequestMethod("GET");
        //设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        //建立实际的连接
        connection.connect();
        //定义BufferReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine = null;
        while ( (inputLine = in.readLine()) != null) {
            json.append(inputLine);
        }
    } catch (MalformedURLException e) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","401");
        e.printStackTrace();
        return jsonObject.toJSONString();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(in!=null){
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return json.toString();
}

2.2 POST请求

public static String loadPOST(String url,String param){
    StringBuilder json = new StringBuilder();
    OutputStreamWriter out = null;
    BufferedReader in = null;
    try {
        URL requestUrl = new URL(url);
        // 打开和URL之间的连接
        HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
        //请求方式
        connection.setRequestMethod("POST");
        //设置通用的请求属性
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        //DoOutput设置是否向httpUrlConnection输出,DoInput设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
        connection.setDoOutput(true);
        connection.setDoInput(true);
        //获取URLConnection对象对应的输出流
        out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
        //发送请求参数即数据
        out.write(param);
        out.flush(); // 立即充刷至请求体)PrintWriter默认先写在内存缓存中
        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine = null;
        while ( (inputLine = in.readLine()) != null) {
            json.append(inputLine);
        }
        connection.disconnect();
    } catch (MalformedURLException e) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","401");
        e.printStackTrace();
        return jsonObject.toJSONString();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            if(out!=null){
                out.close();
            }
            if(in!=null){
                in.close();
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    return json.toString();
}
这篇关于JAVA实现GET和POST请求的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!