远程调用URL的封装方法

private static String post(String postUrl, String jsonParam,String token) throws Exception {
        System.out.println("------方法:post------提示:远程调用(开始),postUrl:"+postUrl);
        String json = "";
        try {
            RequestConfig config = RequestConfig.custom().setSocketTimeout(200000).setConnectTimeout(50000).setConnectionRequestTimeout(50000).build();
            HttpClient client = HttpClientBuilder.create().build();
            HttpPost post = new HttpPost(postUrl);
            post.setHeader("Content-type", "application/json; charset=utf-8");
            post.setHeader("Connection", "Close");
            post.setHeader("token",token);
            StringEntity entity = new StringEntity(jsonParam, Charset.forName("UTF-8"));
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            post.setEntity(entity);
            post.setConfig(config);
            HttpResponse response = client.execute(post);
            System.out.println(response);
            
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                json = EntityUtils.toString(resEntity, "utf-8");
            }
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getStackTrace());
            throw new RuntimeException("post请求异常发生");
        } catch (ClientProtocolException e) {
            System.out.println(e.getStackTrace());
            throw new RuntimeException("post请求异常发生");
        } catch (ParseException e) {
            System.out.println(e.getStackTrace());
            throw new RuntimeException("post请求异常发生");
        } catch (IOException e) {
            System.out.println(e.getStackTrace());
            throw new RuntimeException("post请求异常发生");
        }
        System.out.println("------方法:post------提示:远程调用(结束),result:" + json);
        return json;
    }

你可能感兴趣的:(远程调用URL的封装方法)