• httpclient raw请求


    最近开发中需要从一个第三方系统中获取数据,使用到了httpclient方法:

    httpclient raw请求:

        /**
         * java发送raw
         * @url 请求地址
         * @param 请求参数
         * @return 返回响应内容
         */
        public    
        public
    public static String rawPost(String url,String param) {
    
            //HttpClients.createDefault()等价于 HttpClientBuilder.create().build();   
            CloseableHttpClient closeableHttpClient = HttpClients.createDefault();   
            HttpPost httpost = new HttpPost(url);  
            //JSONObject jsonString = JSON.parseObject(param);
            //设置header
            httpost.setHeader("Content-type", "application/json");    
            httpost.addHeader("appid", "502");
            httpost.addHeader("username", "menhu");
            //组织请求参数  
            StringEntity stringEntity = new StringEntity(param);  
            httpost.setEntity(stringEntity);  
            String content = null;  
            CloseableHttpResponse  httpResponse = null;  
            try {  
                //响应信息
                httpResponse = closeableHttpClient.execute(httpost);  
                HttpEntity entity = httpResponse.getEntity();  
                content = EntityUtils.toString(entity);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }finally{  
                try {  
                    httpResponse.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            try {  //关闭连接、释放资源  
                closeableHttpClient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }    
            return content; 
        }

    客户端获取请求的参数

    注意事项:获取请求参数时使用request.getParameter无法获取参数,需要使用流的方式来获取具体的请求参数:

  • 相关阅读:
    关于 java jdk 环境变量的配置
    Jquery ajax 参数 详解
    关于百度world 编辑器改变上传图片的保存路径图片不显示的问题
    asp.net 中日期的格式化显示的方法
    sql server数据库中 smallint, int ,bigint ,tinyint的区别与长度
    create sequence
    INSERT高级应用
    TRUNCATE TABLE
    CREATE DATABASE LINK
    bulk
  • 原文地址:https://www.cnblogs.com/xiamengz/p/12970869.html
Copyright © 2020-2023  润新知