HttpClient 以post的方式发送请求(由于请求参数太多所以改成以post提交表单的方式)...

1:Util类方法

    /**
     * 发送 Post请求
     * 
     * @param url
     * @param reqXml
     * @return
     */
    public static String post(String url, List<NameValuePair> params) {
        String body = null;
        try {
            // 设置客户端编码
            if (httpClient == null) {
                // Create HttpClient Object
                httpClient = new DefaultHttpClient();
            }

            // Post请求
            HttpPost httppost = new HttpPost(url);

            // 设置参数
            httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            // 发送请求
            HttpResponse httpresponse = httpClient.execute(httppost);
            // 获取返回数据
            HttpEntity entity = httpresponse.getEntity();
            body = EntityUtils.toString(entity,"UTF-8");
            if (entity != null) {
                entity.consumeContent();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return body;
    }

2:调用接口

                //调用接口
                NameValuePair json = new BasicNameValuePair("json",jsonData);
                NameValuePair mobile = new BasicNameValuePair("mobile",stallApply.getMobile());
                NameValuePair templateCode = new BasicNameValuePair("templateCode","SMS_74665033");
                NameValuePair ntimestamp = new BasicNameValuePair("timestamp",timestamp);
                NameValuePair nsignature = new BasicNameValuePair("signature",signature);
                String url = orderDomain+"/code/sendMsmMsg";
                List<NameValuePair> list = new ArrayList<NameValuePair>();
                list.add(json);
                list.add(mobile);
                list.add(templateCode);
                list.add(ntimestamp);
                list.add(nsignature);
                String data = HttpClientUtil.post(url,list);
                System.out.println(data);

 

转载于:https://www.cnblogs.com/SHMILYHP/p/7116698.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发送 POST 请求时,可以通过设置请求体来传递参数。如果参数是整数类型,可以将其转换为字符串类型并设置到请求体中。以下是使用 Apache HttpClient 发送带有整数参数POST 请求的示例代码: ``` import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.net.URI; import java.util.ArrayList; import java.util.List; public class HttpClientPostExample { public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); // 设置请求参数 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("param1", "123")); // 构建请求体 HttpEntity entity = new StringEntity("param1=123", ContentType.APPLICATION_FORM_URLENCODED); // 构建请求 URI URI uri = new URIBuilder() .setScheme("http") .setHost("example.com") .setPath("/api") .build(); // 构建 POST 请求 HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); // 执行请求 CloseableHttpResponse response = httpClient.execute(httpPost); try { HttpEntity responseEntity = response.getEntity(); String result = EntityUtils.toString(responseEntity); System.out.println(result); } finally { response.close(); } } } ``` 在上面的示例代码中,我们将整数参数设置为字符串类型,并将其放入请求体中。在构建请求体时,我们使用了 ContentType.APPLICATION_FORM_URLENCODED 类型,该类型表示请求体中包含 URL 编码的表单参数。在执行请求后,我们将响应结果转换为字符串并打印出来。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值