远程调用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;
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容