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无法获取参数,需要使用流的方式来获取具体的请求参数:

  • 相关阅读:
    声明、定义本质的区别:有无内存的分配
    typedef 与 define 的区别
    Linux内核中链表的学习
    C语言数据类型的转换
    状态机
    170311php添加留言页面
    170314网络编程之TCP聊天窗口
    php课堂2简单作业+文件上传之案例
    php案例2——用户列表页
    学生管理系统
  • 原文地址:https://www.cnblogs.com/xiamengz/p/12970869.html
Copyright © 2011-2022 走看看