zoukankan      html  css  js  c++  java
  • 接口工具类2


    import java.io.IOException;
    import java.io.UnsupportedEncodingException;

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.methods.RequestEntity;
    import org.apache.commons.httpclient.methods.StringRequestEntity;

    public class HttpYSBIS {
        /**
         * 发送POST请求HttpMethod
         *
         * @param url
         *            请求的地址
         * @param parameter
         *            请求的参数
         * @return
         * @throws Exception
         */
        public static String getPostMethod(String url, String parameter) throws Exception {
            HttpClient client = new HttpClient();
            PostMethod post = new PostMethod(url);
            try {
                RequestEntity entity = new StringRequestEntity(parameter.replaceAll("\+", "%2B"), "application/json","UTF-8");
                // application/x-www-form-urlencoded方式是Jquery的Ajax请求默认方式,这种方式的好处就是浏览器都支持,在请求发送过程中会对数据进行序列化处理
                post.setRequestHeader("Content-Type","application/json;charset=UTF-8");
            
                post.setRequestEntity(entity);
                // 使用 POST 方式提交数据
                client.executeMethod(post);
                // 打印服务器返回的状态
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            // 打印结果页面
            String response = new String(post.getResponseBodyAsString().getBytes("UTF-8"),"UTF-8");
            new String(post.getResponseBodyAsString());
            // 打印返回的信息
            post.releaseConnection();
            return response;
        }
        
        
        public static void main(String[] args) {
            String str="{'dateStr':'2019-04-03 16:31','OAdh':'LFDJ20190403001','userId':'180312139','message':'金牛游戏有限公司000(111)'}";
        
            try {
                String a = HttpYSBIS.getPostMethod("接口地址及方法",str);
                System.out.println(a);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }

    }

  • 相关阅读:
    统计次数
    使用正则消除行号
    【收集】sql查询统计,周,月,年
    ASP.NET脚本过滤-防止跨站脚本攻击(收集别人的)
    win10环境下jdk1.8+Android Developer Tools Build: v22.3.0-887826的问题
    关于虚拟机的问题解决(转自豆瓣)
    工作
    numpy学习
    deepin Python pycharm安装
    pymysql连接和操作Mysql数据库
  • 原文地址:https://www.cnblogs.com/cjxns/p/11125722.html
Copyright © 2011-2022 走看看