Java Code Examples for org.apache.http.util.EntityUtils#toString()
The following examples show how to use
org.apache.http.util.EntityUtils#toString() .
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: WechatPay2Credentials.java From wechatpay-apache-httpclient with Apache License 2.0 | 6 votes |
protected final String buildMessage(String nonce, long timestamp, HttpRequestWrapper request) throws IOException { URI uri = request.getURI(); String canonicalUrl = uri.getRawPath(); if (uri.getQuery() != null) { canonicalUrl += "?" + uri.getRawQuery(); } String body = ""; // PATCH,POST,PUT if (request.getOriginal() instanceof WechatPayUploadHttpPost) { body = ((WechatPayUploadHttpPost) request.getOriginal()).getMeta(); } else if (request instanceof HttpEntityEnclosingRequest) { body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity()); } return request.getRequestLine().getMethod() + "\n" + canonicalUrl + "\n" + timestamp + "\n" + nonce + "\n" + body + "\n"; }
Example 2
Source File: HttpClientUtil.java From xmfcn-spring-cloud with Apache License 2.0 | 6 votes |
/** * 发送get请求 * * @param url 路径 * @return */ public static JSONObject httpGet(String url) { // get请求返回结果 JSONObject jsonResult = null; try { HttpClient client = HttpClientBuilder.create().build(); // 发送get请求 HttpGet request = new HttpGet(url); request.addHeader("", ""); HttpResponse response = client.execute(request); /** 请求发送成功,并得到响应 **/ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { /** 读取服务器返回过来的json字符串数据 **/ String strResult = EntityUtils.toString(response.getEntity()); /** 把json字符串转换成json对象 **/ jsonResult = JSONObject.parseObject(strResult); url = URLDecoder.decode(url, "UTF-8"); } else { logger.error("get请求提交失败:" + url); } } catch (IOException e) { logger.error("get请求提交失败:" + url, e); } return jsonResult; }
Example 3
Source File: BaseExceptionResponseHandler.java From bboss-elasticsearch with Apache License 2.0 | 6 votes |
protected Object handleException(String url,HttpEntity entity ,int status) throws IOException { if(status == 404){//在有些场景下面,404不能作为异常抛出,这里作一次桥接,避免不必要的exception被apm性能监控工具探测到 if (entity != null) { if(_logger.isDebugEnabled()) { _logger.debug(new StringBuilder().append("Request url:").append(url).append(",status:").append(status).toString()); } this.elasticSearchException = new ElasticSearchException(EntityUtils.toString(entity), status); } else this.elasticSearchException = new ElasticSearchException(new StringBuilder().append("Request url:").append(url).append(",Unexpected response status: ").append( status).toString(), status); return null; } else { if (entity != null) { if (_logger.isDebugEnabled()) { _logger.debug(new StringBuilder().append("Request url:").append(url).append(",status:").append(status).toString()); } throw new ElasticSearchException(EntityUtils.toString(entity), status); } else throw new ElasticSearchException(new StringBuilder().append("Request url:").append(url).append(",Unexpected response status: ").append( status).toString(), status); } }
Example 4
Source File: TokenServiceHttpsJwt.java From api-layer with Eclipse Public License 2.0 | 6 votes |
private String extractToken(CloseableHttpResponse response) throws ZaasClientException, IOException { String token = ""; int httpResponseCode = response.getStatusLine().getStatusCode(); if (httpResponseCode == 204) { HeaderElement[] elements = response.getHeaders("Set-Cookie")[0].getElements(); Optional<HeaderElement> apimlAuthCookie = Stream.of(elements) .filter(element -> element.getName().equals(COOKIE_PREFIX)) .findFirst(); if (apimlAuthCookie.isPresent()) { token = apimlAuthCookie.get().getValue(); } } else { String obtainedMessage = EntityUtils.toString(response.getEntity()); if (httpResponseCode == 401) { throw new ZaasClientException(ZaasClientErrorCodes.INVALID_AUTHENTICATION, obtainedMessage); } else if (httpResponseCode == 400) { throw new ZaasClientException(ZaasClientErrorCodes.EMPTY_NULL_USERNAME_PASSWORD, obtainedMessage); } else { throw new ZaasClientException(ZaasClientErrorCodes.GENERIC_EXCEPTION, obtainedMessage); } } return token; }
Example 5
Source File: MyClawer.java From java-Crawler with MIT License | 6 votes |
public static synchronized void printHot(String u) throws Exception { HttpHost httpHost = new HttpHost( "60.195.17.240",3128); HttpPost httpPost = new HttpPost(u); httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"); List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("params", "RlBC7U1bfy/boPwg9ag7/a7AjkQOgsIfd+vsUjoMY2tyQCPFgnNoxHeCY+ZuHYqtM1zF8DWIBwJWbsCOQ6ZYxBiPE3bk+CI1U6Htoc4P9REBePlaiuzU4M3rDAxtMfNN3y0eimeq3LVo28UoarXs2VMWkCqoTXSi5zgKEKbxB7CmlBJAP9pn1aC+e3+VOTr0")); list.add(new BasicNameValuePair("encSecKey", "76a0d8ff9f6914d4f59be6b3e1f5d1fc3998317195464f00ee704149bc6672c587cd4a37471e3a777cb283a971d6b9205ce4a7187e682bdaefc0f225fb9ed1319f612243096823ddec88b6d6ea18f3fec883d2489d5a1d81cb5dbd0602981e7b49db5543b3d9edb48950e113f3627db3ac61cbc71d811889d68ff95d0eba04e9")); httpPost.setEntity(new UrlEncodedFormEntity(list)); CloseableHttpResponse response = closeableHttpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String ux = EntityUtils.toString(entity, "utf-8"); //System.out.println(ux); ArrayList<String> s = getBook(ux); if(s.size()!=0) { System.out.println(Thread.currentThread().getName() + " ::: " + "目标:" + u); randomAccessFile.write((Thread.currentThread().getName() + " ::: " + "目标:" + u+"\r\n").getBytes()); } for (int i = 0; i < s.size(); i++) { String[] arr = s.get(i).split("\""); System.out.println(" "+arr[2]); randomAccessFile.write((arr[2]+"\r\n").getBytes()); } }
Example 6
Source File: ConanProxyIT.java From nexus-repository-conan with Eclipse Public License 1.0 | 6 votes |
@Test public void retrieveDownloadUrls() throws Exception { HttpResponse response = proxyClient.getHttpResponse(PATH_DOWNLOAD_URLS); assertThat(status(response), is(HttpStatus.OK)); HttpEntity entity = response.getEntity(); String download_urls = EntityUtils.toString(entity); JsonObject obj = new JsonParser().parse(download_urls).getAsJsonObject(); assertThat(obj.get(FILE_INFO).getAsString(), is(NXRM_CONAN_PROXY_REPO_PATH + PATH_INFO)); assertThat(obj.get(FILE_PACKAGE).getAsString(), is(NXRM_CONAN_PROXY_REPO_PATH + PATH_TGZ_PACKAGE)); assertThat(obj.get(FILE_MANIFEST).getAsString(), is(NXRM_CONAN_PROXY_REPO_PATH + PATH_MANIFEST)); final Asset asset = findAsset(proxyRepo, PATH_DOWNLOAD_URLS); assertThat(asset.format(), is(ConanFormat.NAME)); assertThat(asset.name(), is(PATH_DOWNLOAD_URLS)); assertThat(asset.contentType(), is(ContentTypes.TEXT_PLAIN)); }
Example 7
Source File: DingTalkUtil.java From xmfcn-spring-cloud with Apache License 2.0 | 6 votes |
/** * send:(向钉钉发送信息) * @author: airufei * @date:2018/1/3 17:13 * @param webhook 钉钉地址 * @param message 发送的消息内容 * @return: */ public static SendResult send(String webhook, Message message) throws IOException { HttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(webhook); httppost.addHeader("Content-Type", "application/json; charset=utf-8"); StringEntity se = new StringEntity(message.toJsonString(), "utf-8"); httppost.setEntity(se); SendResult sendResult = new SendResult(); HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(response.getEntity()); JSONObject obj = JSONObject.parseObject(result); Integer errcode = obj.getInteger("errcode"); sendResult.setErrorCode(errcode); sendResult.setErrorMsg(obj.getString("errmsg")); sendResult.setIsSuccess(errcode.equals(0)); } return sendResult; }
Example 8
Source File: HttpUtil.java From flink-learning with Apache License 2.0 | 6 votes |
public static String doPostString(String url, String jsonParams) throws Exception { CloseableHttpResponse response = null; HttpPost httpPost = new HttpPost(url); String httpStr; try { StringEntity entity = new StringEntity(jsonParams, "UTF-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); httpPost.setHeader("content-type", "application/json"); //如果要设置 Basic Auth 的话 // httpPost.setHeader("Authorization", getHeader()); response = httpClient.execute(httpPost); httpStr = EntityUtils.toString(response.getEntity(), "UTF-8"); } finally { if (response != null) { EntityUtils.consume(response.getEntity()); response.close(); } } return httpStr; }
Example 9
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 10
Source File: HttpUtils.java From common-mvc with MIT License | 5 votes |
/** * 默认使用表单方式提交;如提交JSON请指定content-type * @param url * @param query url方式传参 * @param params 表单方式传参 * @param header * @return * @throws HttpException * @throws IOException */ public String requestHttpPost(String url, Map<String,String> query, Map<String,String> params, Map<String, String> header) { if(null != query) { url = makeQueryUrl(url, query); } HttpPost request = new HttpPost(url); request.setConfig(requestConfig); String responseData = null; //设置header if(null != header) { for(String key : header.keySet()) { request.setHeader(key, String.valueOf(header.get(key))); } } setRequestBody(request, params, header); try{ HttpResponse response = client.execute(request); responseData = EntityUtils.toString(response.getEntity());//获得返回的结果 }catch(IOException e){ // e.printStackTrace(); request.abort(); }finally {} return responseData; }
Example 11
Source File: QueuesRestApiTest.java From ballerina-message-broker with Apache License 2.0 | 5 votes |
@Parameters({"admin-username", "admin-password"}) @Test public void testQueueRetrieval(String username, String password) throws IOException { HttpGet httpGet = new HttpGet(apiBasePath + QueuesApiDelegate.QUEUES_API_PATH); ClientHelper.setAuthHeader(httpGet, username, password); CloseableHttpResponse response = client.execute(httpGet); Assert.assertEquals(response.getStatusLine().getStatusCode(), HttpStatus.SC_OK, "Incorrect status code"); String body = EntityUtils.toString(response.getEntity()); QueueMetadata[] queueMetadata = objectMapper.readValue(body, QueueMetadata[].class); Assert.assertTrue(queueMetadata.length > 0, "Queue metadata list shouldn't be empty."); }
Example 12
Source File: PolyvUtil.java From roncoo-education with MIT License | 5 votes |
/** * 上传问题接口 */ public static QuestionResult uploadQuestion(Question question, String writetoken) { try { String url = SystemUtil.POLYV_QUESTION; HttpPost httpPost = new HttpPost(url.trim()); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000) .setConnectionRequestTimeout(3600000).setSocketTimeout(3600000).build(); httpPost.setConfig(requestConfig); JSONArray choices = new JSONArray(); for (String value : question.getAnswerList()) { JSONObject answer = new JSONObject(); answer.put("answer", value); choices.add(answer); } JSONObject righeAnswer = new JSONObject(); righeAnswer.put("answer", question.getRightAnswer()); righeAnswer.put("right_answer", question.getRight()); choices.add(righeAnswer); List<BasicNameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("method", "saveExam")); nvps.add(new BasicNameValuePair("writetoken", writetoken)); nvps.add(new BasicNameValuePair("vid", question.getVid())); nvps.add(new BasicNameValuePair("examId", question.getExamId())); nvps.add(new BasicNameValuePair("seconds", String.valueOf(question.getSeconds()))); nvps.add(new BasicNameValuePair("question", question.getQuestion())); nvps.add(new BasicNameValuePair("choices", choices.toString())); nvps.add(new BasicNameValuePair("skip", String.valueOf(question.isSkip()))); nvps.add(new BasicNameValuePair("answer", question.getAnswer())); nvps.add(new BasicNameValuePair("wrongShow", String.valueOf(question.getWrongShow()))); nvps.add(new BasicNameValuePair("wrongTime", String.valueOf(question.getWrongTime()))); StringEntity se = new UrlEncodedFormEntity(nvps, CHARSET_UTF_8); httpPost.setEntity(se); HttpResponse httpResponse = HttpClientBuilder.create().build().execute(httpPost); String resultStr = EntityUtils.toString(httpResponse.getEntity(), CHARSET_UTF_8); return (QuestionResult) JSONObject.toBean(JSONObject.fromObject(resultStr), QuestionResult.class); } catch (Exception e) { logger.error("添加问题失败!"); } return null; }
Example 13
Source File: GetUrl.java From java-Crawler with MIT License | 5 votes |
public static void main(String[] args) throws Exception { CloseableHttpClient closeableHttpClient = HttpClients.createDefault() ; HttpPost httpPost = new HttpPost("https://www.zhihu.com/question/263398393/answer/269329988") ; CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost) ; HttpEntity httpEntity = closeableHttpResponse.getEntity() ; String c = EntityUtils.toString(httpEntity) ; System.out.println(c); }
Example 14
Source File: HttpsUtils.java From singleton with Eclipse Public License 2.0 | 5 votes |
private static String execute(HttpUriRequest request) throws Exception { CloseableHttpResponse response = httpsClientExe.execute(request); if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) { throw new Exception("Invalid http status code:" + response.getStatusLine().getStatusCode()); } return EntityUtils.toString(response.getEntity(), Consts.UTF_8); }
Example 15
Source File: ESManager.java From pacbot with Apache License 2.0 | 5 votes |
/** * Fetch current count stats for asset groups. * * @param date * the date * @return the map */ @SuppressWarnings("unchecked") public static Map<String, Map<String, Map<String, Object>>> fetchCurrentCountStatsForAssetGroups(String date) { Map<String, Map<String, Map<String, Object>>> asgInfoList = new HashMap<>(); try { ObjectMapper objMapper = new ObjectMapper(); String payLoad = "{\"query\": { \"match\": { \"date\": \"" + date + "\"} }}"; Response response = invokeAPI("GET", "assetgroup_stats/count_type/_search?size=10000", payLoad); String responseJson = EntityUtils.toString(response.getEntity()); Map<String, Object> docMap = objMapper.readValue(responseJson, new TypeReference<Map<String, Object>>() { }); List<Map<String, Object>> docs = (List<Map<String, Object>>) ((Map<String, Object>) docMap.get("hits")) .get("hits"); for (Map<String, Object> doc : docs) { Map<String, Object> _doc = (Map<String, Object>) doc.get("_source"); Map<String, Map<String, Object>> typeInfo = asgInfoList.get(_doc.get("ag").toString()); if (typeInfo == null) { typeInfo = new HashMap<>(); asgInfoList.put(_doc.get("ag").toString(), typeInfo); } typeInfo.put(_doc.get("type").toString(), _doc); _doc.remove("ag"); _doc.remove("type"); } } catch (ParseException | IOException e) { LOGGER.error("Error in fetchCurrentCountStatsForAssetGroups" ,e ); } return asgInfoList; }
Example 16
Source File: RequestProcessor.java From cellery-distribution with Apache License 2.0 | 5 votes |
/** * Execute http post request * * @param url url * @param contentType content type * @param acceptType accept type * @param authHeader authorization header * @param payload post payload * @return Closable http response * @throws APIException Api exception when an error occurred */ public String doPost( String url, String contentType, String acceptType, String authHeader, String payload) throws APIException { String returnObj = null; try { if (log.isDebugEnabled()) { log.debug("Post payload: " + payload); log.debug("Post auth header: " + authHeader); } StringEntity payloadEntity = new StringEntity(payload); HttpPost httpPost = new HttpPost(url); httpPost.setHeader(Constants.Utils.HTTP_CONTENT_TYPE, contentType); httpPost.setHeader(Constants.Utils.HTTP_RESPONSE_TYPE_ACCEPT, acceptType); httpPost.setHeader(Constants.Utils.HTTP_REQ_HEADER_AUTHZ, authHeader); httpPost.setEntity(payloadEntity); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String responseStr = EntityUtils.toString(entity); int statusCode = response.getStatusLine().getStatusCode(); if (log.isDebugEnabled()) { log.debug("Response status code: " + statusCode); log.debug("Response string : " + responseStr); } if (responseValidate(statusCode, responseStr)) { returnObj = responseStr; } closeClientConnection(); } catch (IOException e) { String errorMessage = "Error occurred while executing the http Post connection."; log.error(errorMessage, e); throw new APIException(errorMessage, e); } return returnObj; }
Example 17
Source File: ComplianceRepositoryImpl.java From pacbot with Apache License 2.0 | 5 votes |
private void bulkUpload(List<String> errors, String bulkRequest) { try { Response resp = invokeAPI("POST", "/_bulk?refresh=true", bulkRequest); String responseStr = EntityUtils.toString(resp.getEntity()); if (responseStr.contains("\"errors\":true")) { logger.error(responseStr); errors.add(responseStr); } } catch (Exception e) { logger.error("Bulk upload failed",e); errors.add(e.getMessage()); } }
Example 18
Source File: AggregateWithJSONAndMaxMinLimits.java From micro-integrator with Apache License 2.0 | 5 votes |
private String sendRequest(String requestUrl) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet get = new HttpGet(requestUrl); get.addHeader("Content-Type", "application/json"); HttpResponse response = httpClient.execute(get); return EntityUtils.toString(response.getEntity(), "UTF-8"); } catch (IOException e) { e.printStackTrace(); } return null; }
Example 19
Source File: StringResponseHandler.java From pardot-java-client with MIT License | 5 votes |
@Override public String handleResponse(final HttpResponse response) throws IOException { final HttpEntity entity = response.getEntity(); final String responseStr = entity != null ? EntityUtils.toString(entity) : null; // Fully consume entity. EntityUtils.consume(entity); // Construct return object return responseStr; }
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; }