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

    }

  • 相关阅读:
    android开发中如何开启用户安装的应用程序?
    丑数查找算法
    session.save_path目录大量session临时文件带来的服务器效率问题
    MOSS点滴(1):如何开发和部署feature
    如何将Excel中两个单元格或两列中的数据合并
    如何在 MOSS 2007 启用 Session
    MOSS LIST的一些属性说明
    国外广播电台
    Excel 导出 按钮
    在文档库或 Windows SharePoint Services SharePoint Portal Server 中创建一个新的文件夹或新文档时,您会收到一个"指定的文件或文件夹名太长"错误消息
  • 原文地址:https://www.cnblogs.com/cjxns/p/11125722.html
Copyright © 2011-2022 走看看