http post请求的两种写法

1、传参形式

public static String sendPostRequest(String url, String postStr) {
        String result = "";

        try {
            HttpClient client = new HttpClient();
            PostMethod method = new PostMethod(url);
            HttpMethodParams params = method.getParams();
            params.setContentCharset("UTF-8");
            method.addParameter("requesttype", requestType);
            if (!StringUtil.isEmpty(postStr)) {
                method.addParameter("data", postStr);
            }

            client.getHttpConnectionManager().getParams().setConnectionTimeout(100000);
            client.getHttpConnectionManager().getParams().setSoTimeout(100000);
            method.getParams().setParameter("http.socket.timeout", 100000);
            int status = client.executeMethod(method);
            if (status == 200) {
                result = method.getResponseBodyAsString();
            }

            method.releaseConnection();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return result;
    }

2、json

public static String postURL(String address, String postStr, String encode) {
        String rec_string = "";
        URL url = null;
        HttpURLConnection urlConn = null;

        try {
            url = new URL(address);
            urlConn = (HttpURLConnection)url.openConnection();
            urlConn.setConnectTimeout(30000);
            urlConn.setReadTimeout(30000);
            urlConn.setRequestMethod("POST");
            urlConn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            urlConn.setRequestProperty("accept", "application/json");
            urlConn.setDoOutput(true);
            urlConn.setDoInput(true);
            OutputStreamWriter out = new OutputStreamWriter(urlConn.getOutputStream(), encode);
            out.write(postStr);
            out.flush();
            out.close();
            BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), encode));
            StringBuffer sb = new StringBuffer();

            int ch;
            while((ch = rd.read()) > -1) {
                sb.append((char)ch);
            }

            rec_string = sb.toString().trim();
            rd.close();
        } catch (Exception var13) {
            rec_string = "-107";
        } finally {
            if (urlConn != null) {
                urlConn.disconnect();
            }

        }
        return rec_string;
    }

传递json的另一种写法:

public String doPost(String url, String jsonStr) {
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        String result = "";

        try {
            StringEntity s = new StringEntity(jsonStr);
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            post.setEntity(s);
            HttpResponse res = httpclient.execute(post);
            if (res.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(res.getEntity());
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        return result;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值