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);
  • 相关阅读:
    Gevent高并发网络库精解
    python多线程参考文章
    python多线程
    进程与线程
    golang 微服务以及相关web框架
    微服务实战:从架构到发布
    python 常用库收集
    总结数据科学家常用的Python库
    20个最有用的Python数据科学库
    自然语言处理的发展历程
  • 原文地址:https://www.cnblogs.com/SHMILYHP/p/7116698.html
Copyright © 2011-2022 走看看