java中怎样用post、get、put请求

java中我们有时需要调用第三方的接口,但是第三方的接口都是http形式的,这是需要用post,get,put请求类型请求

1、GET方式请求

public static String httpGet(String getUrl,Map<String, String> getHeaders) throws IOException {
    URL getURL = new URL(getUrl);
    HttpURLConnection connection = (HttpURLConnection) getURL.openConnection();

    connection.setRequestProperty("accept", "*/*");
    connection.setRequestProperty("connection", "Keep-Alive");
    connection.setRequestProperty("user-agent",
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//在get请求中这是能在各个浏览器兼容json
    if(getHeaders != null) {
        for(String pKey : getHeaders.keySet()) {
            connection.setRequestProperty(pKey, getHeaders.get(pKey));
        }
    }
    connection.connect();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuilder sbStr = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        sbStr.append(line);
    }
    bufferedReader.close();
    connection.disconnect();
    return new String(sbStr.toString().getBytes(),"utf-8");
}

2、POST方式请求

    public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException {
        URL postURL = new URL(postUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
        httpURLConnection.setDoOutput(true);                 
        httpURLConnection.setDoInput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setInstanceFollowRedirects(true);
        //application/json x-www-form-urlencoded
        //httpURLConnection.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");
        httpURLConnection.setRequestProperty("Content-Type",  "application/json;charset=utf-8");
        StringBuilder sbStr = new StringBuilder();
        if(postHeaders != null) {
            for(String pKey : postHeaders.keySet()) {
                httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
            }
        }
        if(postEntity != null) {
             JSONObject obj = new JSONObject();
             obj.put("plate",postEntity);
            PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));   
            out.println(obj);  
            out.close();  
            //httpURLConnection.getInputStream()
            BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));  //解决返回值汉字乱码的问题
              
            String inputLine;
            while ((inputLine = in.readLine()) != null) {  
                sbStr.append(inputLine);  
            }  
            in.close();  
        }
        httpURLConnection.disconnect();
        System.out.println("fsdfdf:"+sbStr.toString());
        return new String(sbStr.toString().getBytes(),"utf-8");
    }    

3、PUT请求

    public static String HttpPut(String postUrl,Map<String, String> postHeaders,String postEntity) throws Exception {  
        URL postURL = new URL(postUrl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
        httpURLConnection.setDoOutput(true);                 
        httpURLConnection.setDoInput(true);
        httpURLConnection.setRequestMethod("PUT");
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setInstanceFollowRedirects(true);
        //application/json x-www-form-urlencoded
        //httpURLConnection.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");//表单上传的模式
        httpURLConnection.setRequestProperty("Content-Type",  "application/json;charset=utf-8");//json格式上传的模式
        StringBuilder sbStr = new StringBuilder();
        if(postHeaders != null) {
            for(String pKey : postHeaders.keySet()) {
                httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
            }
        }
        if(postEntity != null) {
             JSONObject obj = new JSONObject();
             obj.put("plate","京NB1314");
            PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));   
            out.println(obj);  
            out.close();  
            BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection  
                    .getInputStream()));  
              
            String inputLine;
            while ((inputLine = in.readLine()) != null) {  
                sbStr.append(inputLine);  
            }  
            in.close();  
        }
        httpURLConnection.disconnect();
        return new String(sbStr.toString().getBytes(),"utf-8");
    } 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值