private static String queryBattle(String url) { StringBuilder builder = new StringBuilder(); HttpClient client = HttpClientBuilder.create().build(); try { HttpGet httpGet = new HttpGet(URIUtil.encodeQuery(url)); HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content, "UTF-8")); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } else { LOGGER.error("Failed to download file"); return null; } } catch (IOException e) { LOGGER.error(e); } return builder.toString(); }
/** * Call recurly API. * * @param <T> the generic type * @param <E> the element type * @param path the path * @param payload the payload * @param responseClass the response class * @param method the method * @param headers the headers * @return the t * @throws RecurlyException the recurly exception */ protected <T, E> T call( String path, E payload, Class<T> responseClass, HttpMethod method, HttpHeaders headers) throws RecurlyException { URI uri = null; ResponseEntity<T> responseEntity = null; T reponse = null; HttpEntity<?> entity = null; try { if (headers == null) { entity = new HttpEntity<>(payload, recurly.getRecurlyHeaders()); } else { entity = new HttpEntity<>(payload, headers); } uri = new URI(URIUtil.encodeQuery(recurly.getRecurlyServerURL() + path, "UTF-8")); getLogger().debug("Calling Recurly URL {}, method: {}", uri.toString(), method.toString()); responseEntity = restTemplate.exchange(uri, method, entity, responseClass); if (responseEntity != null) reponse = responseEntity.getBody(); } catch (URIException | UnsupportedEncodingException | URISyntaxException e) { throw new RecurlyException("Not able to reach recurly. Please check the URL.", e); } catch (RestClientException e) { String err = ((HttpStatusCodeException) e).getResponseBodyAsString(); int code = ((HttpStatusCodeException) e).getStatusCode().value(); publishError(uri.toString(), err, code); RecurlyException ex = handleError(err, code, e); throw ex; } return reponse; }
private String getFormDirUri(Path formPath) { String fileName = formPath.getFileName(); try { fileName = URIUtil.encodeQuery(fileName); } catch (Exception e) { } return formPath.toURI().substring(0, formPath.toURI().lastIndexOf(fileName) - 1); }
// 对Map内所有value作utf8编码,拼接返回结果 public static String toQueryString(Map<?, ?> data) throws UnsupportedEncodingException, URIException { StringBuffer queryString = new StringBuffer(); for (Entry<?, ?> pair : data.entrySet()) { queryString.append(pair.getKey() + "="); queryString.append(URIUtil.encodeQuery((String) pair.getValue(), "UTF-8") + "&"); } if (queryString.length() > 0) { queryString.deleteCharAt(queryString.length() - 1); } return queryString.toString(); }
// Accessors private String getProxyURL(HttpServletRequest httpServletRequest) { // Get the proxy host String stringProxyHostNew = Config.getInstance().getProxyHost(); this.setProxyHost(stringProxyHostNew); // Get the proxy port if specified String stringProxyPortNew = "" + Config.getInstance().getProxyPort(); this.setProxyPort(Integer.parseInt(stringProxyPortNew)); // Get the proxy path if specified String stringProxyPathNew = Config.getInstance().getProxyPath(); if (stringProxyPathNew != null && stringProxyPathNew.length() > 0) { this.setProxyPath(stringProxyPathNew); } // Get the maximum file upload size if specified String stringMaxFileUploadSize = "" + Config.getInstance().getMaxFileUploadSize(); if (stringMaxFileUploadSize != null && stringMaxFileUploadSize.length() > 0) { this.setMaxFileUploadSize(Integer.parseInt(stringMaxFileUploadSize)); } // Set the protocol to HTTP String stringProxyURL = "http://" + this.getProxyHostAndPort(); // Check if we are proxying to a path other that the document root if (!this.getProxyPath().equalsIgnoreCase("")) { stringProxyURL += this.getProxyPath(); } try { // Handle the path given to the servlet stringProxyURL += URIUtil.encodePath(httpServletRequest.getPathInfo()); // Handle the query string if (httpServletRequest.getQueryString() != null) { stringProxyURL += "?" + URIUtil.encodeQuery(httpServletRequest.getQueryString()); } LOG.debug("Proxying " + httpServletRequest.getRequestURL() + " to " + stringProxyURL); return stringProxyURL; } catch (Throwable t) { throw new RuntimeException(t); } }
/** * Returns list with all stations with their ID's from the server. * * @return List<Station> * @throws HttpException * @throws IOException */ public List<Station> getAllStations() { if (stations != null && !stations.isEmpty()) { return stations; } String url = "http://booking.uz.gov.ua/purchase/station/"; char letter; GetMethod get = null; String jsonResp = null; List<Station> myStations = new ArrayList<>(); try { for (int i = 0; i < 32; i++) { letter = (char) (1072 + i); // 1072 - rus 'a' in ASCII String currentUrl = URIUtil.encodeQuery(url + letter); get = new GetMethod(currentUrl); int statusCode = client.executeMethod(get); if (statusCode != HttpStatus.SC_OK) { logger.error("getAllStations method failed. Get method failed: " + get.getStatusLine()); return null; } jsonResp = IOUtils.toString( get.getResponseBodyAsStream(), "UTF-8"); // get.getResponseBodyAsString(); StationsListJson resp = new ObjectMapper().readValue(jsonResp, StationsListJson.class); myStations.addAll(resp.getStations()); } } catch (HttpException e) { logger.error("Could not retrieve stations." + e.getMessage(), e); throw new RuntimeException("Could not retrieve stations", e); } catch (IOException e) { logger.error("Could not retrieve stations." + e.getMessage(), e); throw new RuntimeException("Could not retrieve stations", e); } Collections.sort(myStations); return stations = myStations; }