码迷,mamicode.com
首页 > Web开发 > 详细

HTTP-POST请求

时间:2020-01-15 19:19:45      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:ica   tin   throw   reader   key   输出   seo   ble   property   

public class HttpUtil {

public static String doPost(String url, String param, int timeOut,String miyue)  throws IOException {

    JSONObject oJson = JSONObject.parseObject(param);
    param = oJson.toJSONString();
    EncryptUtils eu = new EncryptUtils();
    String sign = eu.md5Digest(param+timestamp+bssKey).toLowerCase();
    url+="?timestamp="+timestamp+"&sign="+sign;
 PrintWriter out = null;
    OutputStreamWriter out = null;
    BufferedReader in = null;
    String result = "";
    try {
        URL realUrl = new URL(url);

        URLConnection conn = realUrl.openConnection();

        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        conn.setConnectTimeout(timeOut);
        conn.setDoOutput(true);
        conn.setDoInput(true);

     out = new PrintWriter(conn.getOutputStream());
        out =new OutputStreamWriter(conn.getOutputStream(), "UTF-8");

        out.write(param);

/ out.print(param);

        out.flush();
        // 定义BufferedReader输入流来读取URL的响应
        in = new BufferedReader(
                new InputStreamReader(conn.getInputStream(),"utf-8"));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
    } catch (Throwable e) {
        throw new IOException(e);
    }
    // 使用finally块来关闭输出流、输入流
    finally {
        if (out != null) {
            try {
                out.close();
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
        }
    }
    return result;
}

}

HTTP-POST请求

标签:ica   tin   throw   reader   key   输出   seo   ble   property   

原文地址:https://blog.51cto.com/12439947/2467084

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!