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);

 

posted @ 2017-07-04 14:56  阿若蜜意  阅读(7570)  评论(0编辑  收藏  举报