java后台写HTTP请求,做POST和GET请求,处理返回的json字符串

package com.dxr.sdk.http.client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * HTTP请求管理
 * @author zuiweng clq_zuiweng@163.com
 * 2019年7月15日
 */
public class HttpClient {
    
    /**
     *
     * @param request_url 请求地址
     * @param json_string jsonString,将参数封装到json,json.put("","");>>>json.toString();
     * @return
     * @throws Exception
     */
    public static String openPostRequest(String request_url,String json_string) throws Exception{
        String response = postLoadJSON(request_url, json_string);
        return response;
    }
    
    /**
     *
     * @param url 请求地址 http://...?...&...
     * @return
     * @throws Exception
     */
    public static String openGetRequest(String url) throws Exception{
        String response = getLoadJSON(url);
        return response;
    }
    
    /**
     * json数据返回
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    private static String postLoadJSON(String url,String param)throws Exception {
        StringBuffer buffer = new StringBuffer();
        PrintWriter out = null;
        URL openUrl = new URL(url);
        HttpURLConnection connection =(HttpURLConnection) openUrl.openConnection();
        
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        connection.connect();
        
        out = new PrintWriter(connection.getOutputStream());
        out.println(param);
        
        out.flush();
        out.close();
        BufferedReader reader  = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
        
        String inputLine = null;
        while((inputLine=reader.readLine()) != null) {
            buffer.append(inputLine);
        }
        reader.close();
        return buffer.toString();
    }
    
    /**
     * get请求json返回
     * @param url
     * @return
     * @throws Exception
     */
    private static String getLoadJSON(String url) throws Exception{
        StringBuffer buffer = new StringBuffer();
        URL openUrl = new URL(url);
        HttpURLConnection connection =(HttpURLConnection) openUrl.openConnection();
        
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        connection.connect();
        
        BufferedReader reader  = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
        
        String inputLine = null;
        while((inputLine=reader.readLine()) != null) {
            buffer.append(inputLine);
        }
        reader.close();
        return buffer.toString();
    }
    
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值