String getContent(HttpMethod httpMethod) { StringBuilder contentBuilder = new StringBuilder(); if (isZipContent(httpMethod)) { InputStream is = null; GZIPInputStream gzin = null; InputStreamReader isr = null; BufferedReader br = null; try { is = httpMethod.getResponseBodyAsStream(); gzin = new GZIPInputStream(is); isr = new InputStreamReader( gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // ���ö�ȡ���ı����ʽ���Զ������ br = new BufferedReader(isr); char[] buffer = new char[4096]; int readlen = -1; while ((readlen = br.read(buffer, 0, 4096)) != -1) { contentBuilder.append(buffer, 0, readlen); } } catch (Exception e) { log.error("Unzip fail", e); } finally { try { br.close(); } catch (Exception e1) { // ignore } try { isr.close(); } catch (Exception e1) { // ignore } try { gzin.close(); } catch (Exception e1) { // ignore } try { is.close(); } catch (Exception e1) { // ignore } } } else { String content = null; try { content = httpMethod.getResponseBodyAsString(); } catch (Exception e) { log.error("Fetch config error:", e); } if (null == content) { return null; } contentBuilder.append(content); } return contentBuilder.toString(); }
/* * (non-Javadoc) * @see org.jasig.portlet.weather.dao.IWeatherDao#find(java.lang.String) */ public Collection<Location> find(String location) { final String url = FIND_URL .replace("@KEY@", key) .replace("@QUERY@", QuietUrlCodec.encode(location, Constants.URL_ENCODING)); HttpMethod getMethod = new GetMethod(url); InputStream inputStream = null; try { // Execute the method. int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { final String statusText = getMethod.getStatusText(); throw new DataRetrievalFailureException( "get of '" + url + "' failed with status '" + statusCode + "' due to '" + statusText + "'"); } // Read the response body inputStream = getMethod.getResponseBodyAsStream(); List<Location> locations = deserializeSearchResults(inputStream); return locations; } catch (HttpException e) { throw new RuntimeException( "http protocol exception while getting data from weather service from: " + url, e); } catch (IOException e) { throw new RuntimeException( "IO exception while getting data from weather service from: " + url, e); } catch (JAXBException e) { throw new RuntimeException( "Parsing exception while getting data from weather service from: " + url, e); } finally { // try to close the inputstream IOUtils.closeQuietly(inputStream); // release the connection getMethod.releaseConnection(); } }
protected Object getAndDeserialize(String url, TemperatureUnit unit) { HttpMethod getMethod = new GetMethod(url); InputStream inputStream = null; try { // Execute the method. int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { final String statusText = getMethod.getStatusText(); throw new DataRetrievalFailureException( "get of '" + url + "' failed with status '" + statusCode + "' due to '" + statusText + "'"); } // Read the response body inputStream = getMethod.getResponseBodyAsStream(); Weather weather = deserializeWeatherResult(inputStream, unit); return weather; } catch (HttpException e) { throw new RuntimeException( "http protocol exception while getting data from weather service from: " + url, e); } catch (IOException e) { throw new RuntimeException( "IO exception while getting data from weather service from: " + url, e); } catch (JAXBException e) { throw new RuntimeException( "Parsing exception while getting data from weather service from: " + url, e); } catch (ParseException e) { throw new RuntimeException( "Parsing exception while getting data from weather service from: " + url, e); } finally { // try to close the inputstream IOUtils.closeQuietly(inputStream); // release the connection getMethod.releaseConnection(); } }
public JSONObject get(String path) throws Exception { JSONObject result; HttpMethod method = new GetMethod(repoHostname + ":" + repoHostPort + "/api/data/" + path); method.getParams().setCookiePolicy(CookiePolicy.RFC_2109); method.setRequestHeader("Cookie", "jsonhub-store-key=" + getStoreKey()); // client.getState().setCredentials(new AuthScope(null, repoHostPort, "authentication"), new // UsernamePasswordCredentials(memberId, memberPassword)); // method.setDoAuthentication(true); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); throw new GeneralException(method.getStatusLine().toString()); } else { Reader reader = new InputStreamReader(method.getResponseBodyAsStream()); CharArrayWriter cdata = new CharArrayWriter(); char buf[] = new char[BUFFER_SIZE]; int ret; while ((ret = reader.read(buf, 0, BUFFER_SIZE)) != -1) cdata.write(buf, 0, ret); reader.close(); System.out.println("get response: " + cdata.toString()); result = JSONObject.fromObject(cdata.toString()); } method.releaseConnection(); if (result.has("error")) throw new GeneralException(result.getString("error")); return result; }
/** * Executes the method associated with this method director. * * @throws IOException * @throws HttpException */ public void executeMethod(final HttpMethod method) throws IOException, HttpException { if (method == null) { throw new IllegalArgumentException("Method may not be null"); } // Link all parameter collections to form the hierarchy: // Global -> HttpClient -> HostConfiguration -> HttpMethod this.hostConfiguration.getParams().setDefaults(this.params); method.getParams().setDefaults(this.hostConfiguration.getParams()); // Generate default request headers Collection defaults = (Collection) this.hostConfiguration.getParams().getParameter(HostParams.DEFAULT_HEADERS); if (defaults != null) { Iterator i = defaults.iterator(); while (i.hasNext()) { method.addRequestHeader((Header) i.next()); } } try { int maxRedirects = this.params.getIntParameter(HttpClientParams.MAX_REDIRECTS, 100); for (int redirectCount = 0; ; ) { // make sure the connection we have is appropriate if (this.conn != null && !hostConfiguration.hostEquals(this.conn)) { this.conn.setLocked(false); this.conn.releaseConnection(); this.conn = null; } // get a connection, if we need one if (this.conn == null) { this.conn = connectionManager.getConnectionWithTimeout( hostConfiguration, this.params.getConnectionManagerTimeout()); this.conn.setLocked(true); if (this.params.isAuthenticationPreemptive() || this.state.isAuthenticationPreemptive()) { LOG.debug("Preemptively sending default basic credentials"); method.getHostAuthState().setPreemptive(); method.getHostAuthState().setAuthAttempted(true); if (this.conn.isProxied() && !this.conn.isSecure()) { method.getProxyAuthState().setPreemptive(); method.getProxyAuthState().setAuthAttempted(true); } } } authenticate(method); executeWithRetry(method); if (this.connectMethod != null) { fakeResponse(method); break; } boolean retry = false; if (isRedirectNeeded(method)) { if (processRedirectResponse(method)) { retry = true; ++redirectCount; if (redirectCount >= maxRedirects) { LOG.error("Narrowly avoided an infinite loop in execute"); throw new RedirectException("Maximum redirects (" + maxRedirects + ") exceeded"); } if (LOG.isDebugEnabled()) { LOG.debug("Execute redirect " + redirectCount + " of " + maxRedirects); } } } if (isAuthenticationNeeded(method)) { if (processAuthenticationResponse(method)) { LOG.debug("Retry authentication"); retry = true; } } if (!retry) { break; } // retry - close previous stream. Caution - this causes // responseBodyConsumed to be called, which may also close the // connection. if (method.getResponseBodyAsStream() != null) { method.getResponseBodyAsStream().close(); } } // end of retry loop } finally { if (this.conn != null) { this.conn.setLocked(false); } // If the response has been fully processed, return the connection // to the pool. Use this flag, rather than other tests (like // responseStream == null), as subclasses, might reset the stream, // for example, reading the entire response into a file and then // setting the file as the stream. if ((releaseConnection || method.getResponseBodyAsStream() == null) && this.conn != null) { this.conn.releaseConnection(); } } }