httpclient raw请求


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

httpclient raw请求:

    /**
     * java发送raw
     * @url 请求地址
     * @param 请求参数
     * @return 返回响应内容
     */
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无法获取参数,需要使用流的方式来获取具体的请求参数:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM