HTTP-POST请求

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTTP-POST请求相关的知识,希望对你有一定的参考价值。

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请求的主要内容,如果未能解决你的问题,请参考以下文章

在 IIS7 URL 重写模块中,我可以在重定向规则中指定不适用于 http-post 请求吗?

与 isPassive 的 HTTP-Post 绑定

使用 libcurl 进行 http-post

当 HTTP-POST 有 body 时,url 参数为空

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装