zoukankan      html  css  js  c++  java
  • 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无法获取参数,需要使用流的方式来获取具体的请求参数:

  • 相关阅读:
    BootStrap练习
    表单控件练习
    K近邻算法原理
    CSS 边框和颜色
    SVG平移和旋转
    SVG进阶练习
    SVG路标(marker)
    SVG 曲线与文字
    python函数与异常处理
    if-elif-else分支判断语句(附加continue和break)---举例说明
  • 原文地址:https://www.cnblogs.com/xiamengz/p/12970869.html
Copyright © 2011-2022 走看看