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

    }

  • 相关阅读:
    Java基础之IO框架
    Java基础之RTTI 运行时类型识别
    Java进阶之多线程
    Java进阶之内存管理与垃圾回收
    Java进阶之网络编程
    springcloud的Hystrix turbine断路器聚合监控实现(基于springboot2.02版本)
    springmvc,controller层在接收浏览器url传来的参数带中文乱码问题。
    Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins:mav问题
    springboot2.x自定义拦截把static静态文件给拦截的坑
    Eclipse启动发生的错误:An internal error occurred during: "Initializing Java Tooling".
  • 原文地址:https://www.cnblogs.com/cjxns/p/11125722.html
Copyright © 2011-2022 走看看