Java Code Examples for org.apache.http.client.methods.HttpPost#abort()
The following examples show how to use
org.apache.http.client.methods.HttpPost#abort() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebHttpUtils.java From frpMgr with MIT License | 6 votes |
public static String postJson(String url, String json) { String data = null; logger.debug(">>>请求地址:{},参数:{}", url, json); try { HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(json, ENCODE); entity.setContentEncoding(ENCODE); entity.setContentType("application/json"); httpPost.setEntity(entity); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = response.getEntity(); data = EntityUtils.toString(httpEntity, ENCODE); logger.debug(">>>返回结果:{}", data); EntityUtils.consume(httpEntity); } else { httpPost.abort(); } } } catch (Exception e) { logger.error(">>>请求异常", e); } return data; }
Example 2
Source File: HttpService.java From PeonyFramwork with Apache License 2.0 | 6 votes |
/** * post访问 * @param url 地址 * @param body 访问数据 * @param contentType 访问的格式 * @return 访问返回值 */ @Suspendable public String doPost(String url, final String body, ContentType contentType){ HttpPost post = new HttpPost(url); String str = ""; try { StringEntity strEn = new StringEntity(body, contentType); post.setEntity(strEn); HttpResponse response = httpclient.execute(post); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instreams = entity.getContent(); str = convertStreamToString(instreams); logger.info("get http post response:{}", str); } }catch (Exception e){ logger.error("post exception:", e); }finally { post.abort(); } return str; }
Example 3
Source File: HttpUtil.java From common-project with Apache License 2.0 | 6 votes |
public static String sendJSONPost(String url, String jsonData, Map<String, String> headers) { String result = null; HttpPost httpPost = new HttpPost(url); StringEntity postEntity = new StringEntity(jsonData, CHARSET_NAME); postEntity.setContentType("application/json"); httpPost.setEntity(postEntity); httpPost.setHeader("Content-Type", "application/json"); HttpClient client = HttpClients.createDefault(); if (headers != null && !headers.isEmpty()) { headers.forEach((k, v) -> { httpPost.setHeader(k, v); }); } try { HttpResponse response = client.execute(httpPost); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { logger.error("http request error ", e); } finally { httpPost.abort(); } return result; }
Example 4
Source File: WebHttpUtils.java From frpMgr with MIT License | 5 votes |
public static String post(String url, Map<String, String> nameValuePair) { String data = null; logger.debug(">>>请求地址:{},参数:{}", url, JSON.toJSONString(nameValuePair)); try { HttpPost httpPost = new HttpPost(url); if (nameValuePair != null) { List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : nameValuePair.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } httpPost.setEntity(new UrlEncodedFormEntity(nvps, ENCODE)); } try (CloseableHttpResponse response = httpClient.execute(httpPost)) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); data = EntityUtils.toString(entity); logger.debug(">>>返回结果:{}", data); EntityUtils.consume(entity); } else { httpPost.abort(); } } } catch (Exception e) { logger.error(">>>请求异常", e); } return data; }
Example 5
Source File: HttpClientUtil.java From charging_pile_cloud with MIT License | 5 votes |
/** * 封装HTTP POST方法 * * @param * @param * @return * @throws Exception */ public static String post(String url, Map<String, String> paramMap) throws Exception { @SuppressWarnings("resource") HttpClient httpClient = new CertificateAuthorityHttpClientUtil(); HttpPost httpPost = new HttpPost(url); List<NameValuePair> formparams = setHttpParams(paramMap); UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8"); httpPost.setEntity(param); HttpResponse response = httpClient.execute(httpPost); String httpEntityContent = getHttpEntityContent(response); httpPost.abort(); return httpEntityContent; }
Example 6
Source File: HttpClientUtil.java From charging_pile_cloud with MIT License | 5 votes |
/** * 封装HTTP POST方法 * * @param * @param (如JSON串) * @return */ public static String post(String url, String data) throws ClientProtocolException, IOException { HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setIntParameter("http.socket.timeout", 5000); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "text/json; charset=utf-8"); httpPost.setEntity(new StringEntity(URLEncoder.encode(data, "UTF-8"))); HttpResponse response = httpClient.execute(httpPost); String httpEntityContent = getHttpEntityContent(response); httpPost.abort(); return httpEntityContent; }
Example 7
Source File: HttpClientUtils.java From wish-pay with Apache License 2.0 | 5 votes |
/** * HTTP Post 获取内容 * * @param url 请求的url地址 ?之前的地址 * @param params 请求的参数 * @param charset 编码格式 * @return 页面内容 */ public static String doPost(String url, Map<String, String> params, String charset) throws Exception { if (StringUtils.isBlank(url)) { return null; } try { List<NameValuePair> pairs = null; if (params != null && !params.isEmpty()) { pairs = new ArrayList<>(params.size()); //去掉NameValuePair转换,这样就可以传递Map<String,Object> /*pairs = new ArrayList<NameValuePair>(params.size());*/ for (Map.Entry<String, String> entry : params.entrySet()) { String value = entry.getValue(); if (value != null) { pairs.add(new BasicNameValuePair(entry.getKey(), value)); } } } HttpPost httpPost = new HttpPost(url); if (pairs != null && pairs.size() > 0) { httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET)); } CloseableHttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpPost.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, "utf-8"); } EntityUtils.consume(entity); response.close(); return result; } catch (Exception e) { throw e; } }
Example 8
Source File: HttpClients.java From flash-waimai with MIT License | 5 votes |
/** * 封装HTTP POST方法 * * @param * @param (如JSON串) * @return * @throws ClientProtocolException * @throws IOException */ public static String post(String url, String data) throws ClientProtocolException, IOException { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "text/json; charset=utf-8"); httpPost.setEntity(new StringEntity(URLEncoder.encode(data, "UTF-8"))); HttpResponse response = httpClient.execute(httpPost); String httpEntityContent = getHttpEntityContent(response); httpPost.abort(); return httpEntityContent; }
Example 9
Source File: HttpUtil.java From SpringMVC-Project with MIT License | 5 votes |
/** * HTTP Post 获取内容 * * @param url 请求的url地址 ?之前的地址 * @param params 请求的参数 * @param charset 编码格式 * @return 页面内容 */ public static String doPost(String url, Map<String, String> params, String charset) { if (StringUtils.isBlank(url)) { return null; } try { List<NameValuePair> pairs = null; if (params != null && !params.isEmpty()) { pairs = Lists.newArrayListWithCapacity(params.size()); for (Map.Entry<String, String> entry : params.entrySet()) { String value = entry.getValue(); if (value != null) { pairs.add(new BasicNameValuePair(entry.getKey(), value)); } } } HttpPost httpPost = new HttpPost(url); if (pairs != null && pairs.size() > 0) { httpPost.setEntity(new UrlEncodedFormEntity(pairs, charset)); } CloseableHttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpPost.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, CHARSET); } EntityUtils.consume(entity); response.close(); return result; } catch (Exception e) { e.printStackTrace(); } return null; }
Example 10
Source File: HttpHelper.java From newblog with Apache License 2.0 | 5 votes |
public String doPostLongWait(String url, List<NameValuePair> pairs, String charset) { if (StringUtils.isBlank(url)) { return null; } log.info(" post url=" + url); try { RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(300000) .setConnectTimeout(30000).build(); HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); if (pairs != null && pairs.size() > 0) { httpPost.setEntity(new UrlEncodedFormEntity(pairs, charset)); } CloseableHttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpPost.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, charset); } EntityUtils.consume(entity); response.close(); return result; } catch (Exception e) { log.error("to request addr=" + url + ", " + e.getMessage()); e.printStackTrace(); } return null; }
Example 11
Source File: HttpClientUtils.java From wish-pay with Apache License 2.0 | 5 votes |
/** * 直接将json串post方式进行发送 * * @param url * @param jsonStr * @return */ public static String doPost(String url, String jsonStr) throws Exception { if (StringUtils.isBlank(url)) { return null; } try { HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(jsonStr, CHARSET)); CloseableHttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpPost.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, "utf-8"); } EntityUtils.consume(entity); response.close(); return result; } catch (Exception e) { throw e; } }
Example 12
Source File: HttpTookit.java From lightconf with GNU General Public License v3.0 | 5 votes |
/** * HTTP Post json * * @param url * @param object * @return */ public static String doPostJson(String url, Object object) { if (StringUtils.isBlank(url)) { return null; } try { String jsonStr = JsonMapper.toJsonString(object); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(jsonStr, CHARSET)); CloseableHttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpPost.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, "utf-8"); } EntityUtils.consume(entity); response.close(); return result; } catch (Exception e) { logger.error("HttpClient post request error : ", e); ResultCode<String> reslut = new ResultCode<String>(); reslut.setCode(Messages.API_ERROR_CODE); reslut.setMsg(Messages.API_ERROR_MSG); return JsonMapper.toJsonString(reslut); } }
Example 13
Source File: HttpHelper.java From newblog with Apache License 2.0 | 5 votes |
public String doPost(String url, List<NameValuePair> pairs, String charset) { if (StringUtils.isBlank(url)) { return null; } log.info(" post url=" + url); try { HttpPost httpPost = new HttpPost(url); if (pairs != null && pairs.size() > 0) { httpPost.setEntity(new UrlEncodedFormEntity(pairs, charset)); } CloseableHttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpPost.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, charset); } EntityUtils.consume(entity); response.close(); return result; } catch (Exception e) { log.error("to request addr=" + url + ", " + e.getMessage()); e.printStackTrace(); } return null; }
Example 14
Source File: HttpClientConnection.java From dubbox-hystrix with Apache License 2.0 | 4 votes |
public void close() throws IOException { HttpPost request = this.request; if (request != null) { request.abort(); } }
Example 15
Source File: HttpClientUtil.java From javabase with Apache License 2.0 | 4 votes |
private static String httpPostGZip(String url, Map<String, Object> paramMap, String code, long timer) { long funTimer = System.currentTimeMillis(); log.info(timer + " request[" + url + "] param[" + (paramMap == null?"NULL":paramMap.toString()) + "]"); String content = null; if(StringUtils.isEmpty(url)) { return null; } else { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost method = new HttpPost(url); method.setHeader("Connection", "close"); method.setHeader("Accept-Encoding", "gzip, deflate"); method.setHeader("Accept", "text/plain"); try { UrlEncodedFormEntity e = new UrlEncodedFormEntity(getParamsList(paramMap), code); method.setEntity(e); HttpResponse response = httpclient.execute(method); int status = response.getStatusLine().getStatusCode(); log.info(timer + " status=" + status); if(status == 200) { boolean httpEntity = false; Header[] headers = response.getHeaders("Content-Encoding"); if(headers != null && headers.length > 0) { Header[] httpEntity1 = headers; int is = headers.length; for(int gzin = 0; gzin < is; ++gzin) { Header isr = httpEntity1[gzin]; if(isr.getValue().toLowerCase().indexOf("gzip") != -1) { httpEntity = true; } } } log.info(timer + " isGzip=" + httpEntity); HttpEntity var28 = response.getEntity(); if(httpEntity) { InputStream var29 = var28.getContent(); GZIPInputStream var30 = new GZIPInputStream(var29); InputStreamReader var31 = new InputStreamReader(var30, code); BufferedReader br = new BufferedReader(var31); StringBuffer sb = new StringBuffer(); String tempbf; while((tempbf = br.readLine()) != null) { sb.append(tempbf); sb.append("\r\n"); } var31.close(); var30.close(); content = sb.toString(); } else { content = EntityUtils.toString(var28); } } else if(status == 500) { HttpEntity var27 = response.getEntity(); content = EntityUtils.toString(var27); } else { method.abort(); } } catch (Exception var25) { method.abort(); log.error(timer + " request[" + url + "] ERROR", var25); } finally { if(!method.isAborted()) { httpclient.getConnectionManager().shutdown(); } } log.info(timer + " request[" + url + "] cost:" + (System.currentTimeMillis() - funTimer)); log.info(timer + " request[" + url + "] response[" + content + "]"); return content; } }
Example 16
Source File: HttpHelper.java From newblog with Apache License 2.0 | 4 votes |
/** * HTTP Post 获取内容 * * @param url 请求的url地址 ?之前的地址 * @param params 请求的参数 * @param charset 编码格式 * @return 页面内容 */ public String doPost(String url, Map<String, String> params, String charset) { if (StringUtils.isBlank(url)) { return null; } log.info(" post url=" + url); try { List<NameValuePair> pairs = null; if (params != null && !params.isEmpty()) { pairs = new ArrayList<NameValuePair>(params.size()); for (Map.Entry<String, String> entry : params.entrySet()) { String value = entry.getValue(); if (value != null) { pairs.add(new BasicNameValuePair(entry.getKey(), value)); } } } HttpPost httpPost = new HttpPost(url); if (pairs != null && pairs.size() > 0) { httpPost.setEntity(new UrlEncodedFormEntity(pairs, charset)); } CloseableHttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpPost.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } HttpEntity entity = response.getEntity(); String result = null; if (entity != null) { result = EntityUtils.toString(entity, charset); } EntityUtils.consume(entity); response.close(); return result; } catch (Exception e) { log.error("to request addr=" + url + ", " + e.getMessage()); e.printStackTrace(); } return null; }
Example 17
Source File: HttpClientConnection.java From dubbox with Apache License 2.0 | 4 votes |
public void close() throws IOException { HttpPost request = this.request; if (request != null) { request.abort(); } }
Example 18
Source File: ApiRequest.java From Kingdee-K3Cloud-Web-Api with GNU General Public License v3.0 | 4 votes |
public void doPost() { HttpPost httpPost = getHttpPost(); try { if (httpPost == null) { return; } httpPost.setEntity(getServiceParameters().toEncodeFormEntity()); this._response = getHttpClient().execute(httpPost); if (this._response.getStatusLine().getStatusCode() == 200) { HttpEntity respEntity = this._response.getEntity(); if (this._currentsessionid == "") { CookieStore mCookieStore = ((AbstractHttpClient) getHttpClient()) .getCookieStore(); List cookies = mCookieStore.getCookies(); if (cookies.size() > 0) { this._cookieStore = mCookieStore; } for (int i = 0; i < cookies.size(); i++) { if (_aspnetsessionkey.equals(((Cookie) cookies.get(i)).getName())) { this._aspnetsessionid = ((Cookie) cookies.get(i)).getValue(); } if (_sessionkey.equals(((Cookie) cookies.get(i)).getName())) { this._currentsessionid = ((Cookie) cookies.get(i)).getValue(); } } } this._responseStream = respEntity.getContent(); this._responseString = streamToString(this._responseStream); if (this._isAsynchronous.booleanValue()) { this._listener.onRequsetSuccess(this); } } } catch (Exception e) { if (this._isAsynchronous.booleanValue()) { this._listener.onRequsetError(this, e); } } finally { if (httpPost != null) { httpPost.abort(); } } }
Example 19
Source File: HttpClientConnection.java From dubbox with Apache License 2.0 | 4 votes |
public void close() throws IOException { HttpPost request = this.request; if (request != null) { request.abort(); } }
Example 20
Source File: HttpsRequest.java From pay with Apache License 2.0 | 3 votes |
public String sendPost(String url, String xmlObj) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException { if (!hasInit) { init(); } String result = null; HttpPost httpPost = new HttpPost(url); //得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别 StringEntity postEntity = new StringEntity(xmlObj, "UTF-8"); httpPost.addHeader("Content-Type", "text/xml"); httpPost.setEntity(postEntity); //设置请求器的配置 httpPost.setConfig(requestConfig); try { HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { httpPost.abort(); } return result; }