zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    python基础总结一
    python解释器介绍与安装
    09 字符编码
    python 九九乘法表
    08 基本数据类型及内置方法
    07 Python语法入门之流程控制
    06 Python语法入门之与用户交互、运算符
    05 Python语法入门之垃圾回收机制
    04 Python语法入门之基本数据类型
    03 python语法入门之变量
  • 原文地址:https://www.cnblogs.com/SHMILYHP/p/7116698.html
Copyright © 2011-2022 走看看