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

原文地址:https://www.cnblogs.com/xiamengz/p/12970869.html