| 378 | } |
| 379 | |
| 380 | public String doPost(String url, Object postData) { |
| 381 | CloseableHttpClient httpClient = null; |
| 382 | CloseableHttpResponse httpResponse = null; |
| 383 | String result = ""; |
| 384 | // 创建httpClient实例 |
| 385 | httpClient = HttpClients.createDefault(); |
| 386 | // 创建httpPost远程连接实例 |
| 387 | HttpPost httpPost = new HttpPost(url); |
| 388 | // 配置请求参数实例 |
| 389 | RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeoutS_)// 设置连接主机服务超时时间 |
| 390 | .setConnectionRequestTimeout(timeoutS_)// 设置连接请求超时时间 |
| 391 | .setSocketTimeout(timeoutS_)// 设置读取数据连接超时时间 |
| 392 | .build(); |
| 393 | // 为httpPost实例设置配置 |
| 394 | httpPost.setConfig(requestConfig); |
| 395 | if(this.http_proto){ |
| 396 | httpPost.setHeader("Content-Type", "application/proto"); |
| 397 | }else{ |
| 398 | httpPost.setHeader("Content-Type", "application/json"); |
| 399 | } |
| 400 | |
| 401 | // 设置请求头 |
| 402 | if(response_compress_flag){ |
| 403 | httpPost.addHeader("Accept-encoding", "gzip"); |
| 404 | if(GLOG_v != null){ |
| 405 | System.out.format("------- Accept-encoding gzip: \n"); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | try { |
| 410 | if(postData instanceof String){ |
| 411 | if(request_compress_flag && ((String)postData).length()>1024){ |
| 412 | try{ |
| 413 | byte[] gzipEncrypt = compress(postData); |
| 414 | httpPost.setEntity(new InputStreamEntity(new ByteArrayInputStream(gzipEncrypt), gzipEncrypt.length)); |
| 415 | httpPost.addHeader("Content-Encoding", "gzip"); |
| 416 | } catch (Exception e) { |
| 417 | e.printStackTrace(); |
| 418 | } |
| 419 | }else{ |
| 420 | httpPost.setEntity(new StringEntity((String)postData, "UTF-8")); |
| 421 | } |
| 422 | |
| 423 | }else{ |
| 424 | if(request_compress_flag && ((byte[])postData).length>1024){ |
| 425 | try{ |
| 426 | byte[] gzipEncrypt = compress(postData); |
| 427 | httpPost.setEntity(new InputStreamEntity(new ByteArrayInputStream(gzipEncrypt), gzipEncrypt.length)); |
| 428 | httpPost.addHeader("Content-Encoding", "gzip"); |
| 429 | } catch (Exception e) { |
| 430 | e.printStackTrace(); |
| 431 | } |
| 432 | }else{ |
| 433 | httpPost.setEntity(new ByteArrayEntity((byte[])postData)); |
| 434 | //httpPost.setEntity(new InputStreamEntity(new ByteArrayInputStream((byte[])postData), ((byte[])postData).length)); |
| 435 | } |
| 436 | } |
| 437 | |