List of usage examples for org.apache.commons.httpclient HttpMethod getStatusLine
public abstract StatusLine getStatusLine();
From source file:org.openhab.binding.nest.internal.messages.AbstractRequest.java
/** * Executes the given <code>url</code> with the given <code>httpMethod</code>. In the case of httpMethods that do * not support automatic redirection, manually handle the HTTP temporary redirect (307) and retry with the new URL. * //w ww. ja va 2 s.c om * @param httpMethod * the HTTP method to use * @param url * the url to execute (in milliseconds) * @param contentString * the content to be sent to the given <code>url</code> or <code>null</code> if no content should be * sent. * @param contentType * the content type of the given <code>contentString</code> * @return the response body or <code>NULL</code> when the request went wrong */ protected final String executeUrl(final String httpMethod, final String url, final String contentString, final String contentType) { HttpClient client = new HttpClient(); HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url); method.getParams().setSoTimeout(HTTP_REQUEST_TIMEOUT); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); for (String httpHeaderKey : HTTP_HEADERS.stringPropertyNames()) { method.addRequestHeader(new Header(httpHeaderKey, HTTP_HEADERS.getProperty(httpHeaderKey))); } // add content if a valid method is given ... if (method instanceof EntityEnclosingMethod && contentString != null) { EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method; InputStream content = new ByteArrayInputStream(contentString.getBytes()); eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType)); } if (logger.isDebugEnabled()) { try { logger.trace("About to execute '" + method.getURI().toString() + "'"); } catch (URIException e) { logger.trace(e.getMessage()); } } try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_ACCEPTED) { // perfectly fine but we cannot expect any answer... return null; } // Manually handle 307 redirects with a little tail recursion if (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) { Header[] headers = method.getResponseHeaders("Location"); String newUrl = headers[headers.length - 1].getValue(); return executeUrl(httpMethod, newUrl, contentString, contentType); } if (statusCode != HttpStatus.SC_OK) { logger.warn("Method failed: " + method.getStatusLine()); } InputStream tmpResponseStream = method.getResponseBodyAsStream(); Header encodingHeader = method.getResponseHeader("Content-Encoding"); if (encodingHeader != null) { for (HeaderElement ehElem : encodingHeader.getElements()) { if (ehElem.toString().matches(".*gzip.*")) { tmpResponseStream = new GZIPInputStream(tmpResponseStream); logger.trace("GZipped InputStream from {}", url); } else if (ehElem.toString().matches(".*deflate.*")) { tmpResponseStream = new InflaterInputStream(tmpResponseStream); logger.trace("Deflated InputStream from {}", url); } } } String responseBody = IOUtils.toString(tmpResponseStream); if (!responseBody.isEmpty()) { logger.trace(responseBody); } return responseBody; } catch (HttpException he) { logger.error("Fatal protocol violation: {}", he.toString()); } catch (IOException ioe) { logger.error("Fatal transport error: {}", ioe.toString()); } finally { method.releaseConnection(); } return null; }
From source file:org.openhab.io.net.http.HttpUtil.java
/** * Executes the given <code>url</code> with the given <code>httpMethod</code> * /*from w w w . java2 s . co m*/ * @param httpMethod the HTTP method to use * @param url the url to execute (in milliseconds) * @param httpHeaders optional HTTP headers which has to be set on request * @param content the content to be send to the given <code>url</code> or * <code>null</code> if no content should be send. * @param contentType the content type of the given <code>content</code> * @param timeout the socket timeout to wait for data * @param proxyHost the hostname of the proxy * @param proxyPort the port of the proxy * @param proxyUser the username to authenticate with the proxy * @param proxyPassword the password to authenticate with the proxy * @param nonProxyHosts the hosts that won't be routed through the proxy * @return the response body or <code>NULL</code> when the request went wrong */ public static String executeUrl(String httpMethod, String url, Properties httpHeaders, InputStream content, String contentType, int timeout, String proxyHost, Integer proxyPort, String proxyUser, String proxyPassword, String nonProxyHosts) { HttpClient client = new HttpClient(); // only configure a proxy if a host is provided if (StringUtils.isNotBlank(proxyHost) && proxyPort != null && shouldUseProxy(url, nonProxyHosts)) { client.getHostConfiguration().setProxy(proxyHost, proxyPort); if (StringUtils.isNotBlank(proxyUser)) { client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPassword)); } } HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url); method.getParams().setSoTimeout(timeout); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); if (httpHeaders != null) { for (String httpHeaderKey : httpHeaders.stringPropertyNames()) { method.addRequestHeader(new Header(httpHeaderKey, httpHeaders.getProperty(httpHeaderKey))); } } // add content if a valid method is given ... if (method instanceof EntityEnclosingMethod && content != null) { EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method; eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType)); } Credentials credentials = extractCredentials(url); if (credentials != null) { client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(AuthScope.ANY, credentials); } if (logger.isDebugEnabled()) { try { logger.debug("About to execute '" + method.getURI().toString() + "'"); } catch (URIException e) { logger.debug(e.getMessage()); } } try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_ACCEPTED) { // perfectly fine but we cannot expect any answer... return null; } if (statusCode != HttpStatus.SC_OK) { logger.warn("Method failed: " + method.getStatusLine()); } InputStream tmpResponseStream = method.getResponseBodyAsStream(); Header encodingHeader = method.getResponseHeader("Content-Encoding"); if (encodingHeader != null) { for (HeaderElement ehElem : encodingHeader.getElements()) { if (ehElem.toString().matches(".*gzip.*")) { tmpResponseStream = new GZIPInputStream(tmpResponseStream); logger.debug("GZipped InputStream from {}", url); } else if (ehElem.toString().matches(".*deflate.*")) { tmpResponseStream = new InflaterInputStream(tmpResponseStream); logger.debug("Deflated InputStream from {}", url); } } } String responseBody = IOUtils.toString(tmpResponseStream); if (!responseBody.isEmpty()) { logger.debug(responseBody); } return responseBody; } catch (HttpException he) { logger.error("Fatal protocol violation: {}", he.toString()); } catch (IOException ioe) { logger.error("Fatal transport error: {}", ioe.toString()); } finally { method.releaseConnection(); } return null; }
From source file:org.owtf.runtime.core.ZestBasicRunner.java
private ZestResponse send(HttpClient httpclient, ZestRequest req) throws IOException { HttpMethod method; URI uri = new URI(req.getUrl().toString(), false); switch (req.getMethod()) { case "GET": method = new GetMethod(uri.toString()); // Can only redirect on GETs method.setFollowRedirects(req.isFollowRedirects()); break;/*from www . j av a2s . c o m*/ case "POST": method = new PostMethod(uri.toString()); break; case "OPTIONS": method = new OptionsMethod(uri.toString()); break; case "HEAD": method = new HeadMethod(uri.toString()); break; case "PUT": method = new PutMethod(uri.toString()); break; case "DELETE": method = new DeleteMethod(uri.toString()); break; case "TRACE": method = new TraceMethod(uri.toString()); break; default: throw new IllegalArgumentException("Method not supported: " + req.getMethod()); } setHeaders(method, req.getHeaders()); for (Cookie cookie : req.getCookies()) { // Replace any Zest variables in the value cookie.setValue(this.replaceVariablesInString(cookie.getValue(), false)); httpclient.getState().addCookie(cookie); } if (req.getMethod().equals("POST")) { // Do this after setting the headers so the length is corrected RequestEntity requestEntity = new StringRequestEntity(req.getData(), null, null); ((PostMethod) method).setRequestEntity(requestEntity); } int code = 0; String responseHeader = null; String responseBody = null; Date start = new Date(); try { this.debug(req.getMethod() + " : " + req.getUrl()); code = httpclient.executeMethod(method); responseHeader = method.getStatusLine().toString() + "\n" + arrayToStr(method.getResponseHeaders()); // httpclient.getParams().setParameter("http.method.response.buffer.warnlimit",new Integer(1000000000)); responseBody = method.getResponseBodyAsString(); } finally { method.releaseConnection(); } // Update the headers with the ones actually sent req.setHeaders(arrayToStr(method.getRequestHeaders())); return new ZestResponse(req.getUrl(), responseHeader, responseBody, code, new Date().getTime() - start.getTime()); }
From source file:org.parosproxy.paros.network.HttpMethodHelper.java
private static String getHttpResponseHeaderAsString(HttpMethod httpMethod) { StringBuilder sb = new StringBuilder(200); String name = null;/* ww w .j a va 2 s . com*/ String value = null; // add status line sb.append(httpMethod.getStatusLine().toString()).append(CRLF); Header[] header = httpMethod.getResponseHeaders(); for (int i = 0; i < header.length; i++) { name = header[i].getName(); value = header[i].getValue(); sb.append(name).append(": ").append(value).append(CRLF); } sb.append(CRLF); return sb.toString(); }
From source file:org.paxle.crawler.http.impl.HttpCrawler.java
public ICrawlerDocument request(URI requestUri) { if (requestUri == null) throw new NullPointerException("URL was null"); this.logger.debug(String.format("Crawling URL '%s' ...", requestUri)); ICrawlerDocument doc = null;/* ww w .j a v a2s. co m*/ HttpMethod method = null; try { final ICrawlerContext ctx = this.contextLocal.getCurrentContext(); // creating an empty crawler-document doc = ctx.createDocument(); doc.setLocation(requestUri); final String uriAsciiString = requestUri.toASCIIString(); /* ============================================================================== * HTTP HEAD request * * first use the HEAD method to determine whether the MIME-type is supported * and to compare the content-length with the maximum allowed download size * (both only if the server provides this information, if not, the file is * fetched) * ============================================================================== */ method = new HeadMethod(uriAsciiString); // automatically follows redirects this.initRequestMethod(method); int statusCode = this.getHttpClient().executeMethod(method); final boolean headUnsupported = (statusCode == HttpStatus.SC_METHOD_FAILURE || statusCode == HttpStatus.SC_METHOD_NOT_ALLOWED); if (!headUnsupported) { if (statusCode != HttpStatus.SC_OK) { // RFC 2616 states that the GET and HEAD methods _must_ be supported by any // general purpose servers (which are in fact the ones we are connecting to here) if (statusCode == HttpStatus.SC_NOT_FOUND) { doc.setStatus(ICrawlerDocument.Status.NOT_FOUND); } else { doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, String.format("Server returned: %s", method.getStatusLine())); } this.logger.warn(String.format("Crawling of URL '%s' failed. Server returned: %s", requestUri, method.getStatusLine())); return doc; } // getting the mimetype and charset Header contentTypeHeader = method.getResponseHeader(HTTPHEADER_CONTENT_TYPE); if (!handleContentTypeHeader(contentTypeHeader, doc)) return doc; // reject the document if content-length is above our limit Header contentLengthHeader = method.getResponseHeader(HTTPHEADER_CONTENT_LENGTH); if (!handleContentLengthHeader(contentLengthHeader, doc)) return doc; // FIXME: we've been redirected, re-enqueue the new URL and abort processing //if (!requestUri.equals(method.getURI())) ; } /* ============================================================================== * HTTP GET request * * secondly - if everything is alright up to now - proceed with getting the * actual document * ============================================================================== */ HttpMethod getMethod = new GetMethod(uriAsciiString); // automatically follows redirects method.releaseConnection(); method = getMethod; this.initRequestMethod(method); // send the request to the server statusCode = this.getHttpClient().executeMethod(method); // check the response status code if (statusCode != HttpStatus.SC_OK) { if (statusCode == HttpStatus.SC_NOT_FOUND) { doc.setStatus(ICrawlerDocument.Status.NOT_FOUND); } else { doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, String.format("Server returned: %s", method.getStatusLine())); } this.logger.warn(String.format("Crawling of URL '%s' failed. Server returned: %s", requestUri, method.getStatusLine())); return doc; } // FIXME: we've been redirected, re-enqueue the new URL and abort processing // if (!requestUri.equals(method.getURI())) ; /* * HTTP Content-Type * - getting the mimetype and charset */ Header contentTypeHeader = method.getResponseHeader(HTTPHEADER_CONTENT_TYPE); if (!handleContentTypeHeader(contentTypeHeader, doc)) return doc; /* * HTTP Content-Length * - Reject the document if content-length is above our limit * * We do this a second time here because some servers may have set the content-length * of the head response to <code>0</code> */ Header contentLengthHeader = method.getResponseHeader(HTTPHEADER_CONTENT_LENGTH); if (!handleContentLengthHeader(contentLengthHeader, doc)) return doc; extractHttpHeaders(method, doc); // externalised into this method to cleanup here a bit // getting the response body InputStream respBody = method.getResponseBodyAsStream(); // handle the content-encoding, i.e. decompress the server's response Header contentEncodingHeader = method.getResponseHeader(HTTPHEADER_CONTENT_ENCODING); try { respBody = handleContentEncoding(contentEncodingHeader, respBody); /* Limit the max allowed length of the content to copy. -1 is used for no limit. * * We need to set a limit if: * a) the user has configured a max-download-size AND * b) the server returned no content-length header */ int copyLimit = (this.maxDownloadSize <= 0 || contentLengthHeader != null) ? -1 : this.maxDownloadSize; // copy the content to file final ICrawlerTools crawlerTools = ctx.getCrawlerTools(); crawlerTools.saveInto(doc, respBody, lrc, copyLimit); doc.setStatus(ICrawlerDocument.Status.OK); this.logger.debug(String.format("Crawling of URL '%s' finished.", requestUri)); } catch (IOException e) { String msg = e.getMessage(); if (msg == null || !msg.equals("Corrupt GZIP trailer")) throw e; setHostSetting(method.getURI().getHost(), PREF_NO_ENCODING); msg = String.format("server sent a corrupt gzip trailer at URL '%s'", requestUri); logger.warn(msg); // FIXME re-enqueue command doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, msg); } finally { respBody.close(); } } catch (NoRouteToHostException e) { this.logger.warn(String.format("Error crawling %s: %s", requestUri, e.getMessage())); doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage()); } catch (UnknownHostException e) { this.logger.warn(String.format("Error crawling %s: Unknown host.", requestUri)); doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage()); } catch (ConnectException e) { this.logger.warn(String.format("Error crawling %s: Unable to connect to host.", requestUri)); doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage()); } catch (ConnectTimeoutException e) { this.logger.warn(String.format("Error crawling %s: %s.", requestUri, e.getMessage())); doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage()); } catch (SocketTimeoutException e) { this.logger.warn(String.format("Error crawling %s: Connection timeout.", requestUri)); doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage()); } catch (CircularRedirectException e) { this.logger.warn(String.format("Error crawling %s: %s", requestUri, e.getMessage())); doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage()); } catch (NoHttpResponseException e) { this.logger.warn(String.format("Error crawling %s: %s", requestUri, e.getMessage())); doc.setStatus(ICrawlerDocument.Status.NOT_FOUND, e.getMessage()); } catch (ContentLengthLimitExceededException e) { this.logger.warn(String.format("Error crawling %s: %s", requestUri, e.getMessage())); doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, e.getMessage()); } catch (Throwable e) { String errorMsg; if (e instanceof HttpException) { errorMsg = "Unrecovered protocol exception: [%s] %s"; } else if (e instanceof IOException) { errorMsg = "Transport exceptions: [%s] %s"; } else { errorMsg = "Unexpected exception: [%s] %s"; } errorMsg = String.format(errorMsg, e.getClass().getName(), e.getMessage()); this.logger.error(String.format("Error crawling %s: %s", requestUri, errorMsg)); doc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, errorMsg); e.printStackTrace(); } finally { if (method != null) method.releaseConnection(); } return doc; }
From source file:org.paxle.filter.robots.impl.RobotsTxtManager.java
/** * Downloads a <i>robots.txt</i> file from the given url and parses it * @param robotsUrlStr the URL to the robots.txt. This must be a http(s) resource * @return the parsed robots.txt file as a {@link RobotsTxt}-object * @throws IOException//from w w w . ja v a2 s.c o m * @throws URISyntaxException */ RobotsTxt getFromWeb(URI robotsURL) throws IOException, URISyntaxException { String hostPort = this.getHostPort(robotsURL); String statusLine = null; if (!robotsURL.getScheme().startsWith("http")) { throw new IOException(String.format("Unsupported protocol: %s", robotsURL.getScheme())); } InputStream inputStream = null; HttpMethod getMethod = null; try { getMethod = new GetMethod(robotsURL.toASCIIString()); int code = this.httpClient.executeMethod(getMethod); statusLine = getMethod.getStatusLine().toString(); if (code == HttpStatus.SC_UNAUTHORIZED || code == HttpStatus.SC_FORBIDDEN) { // access to the whole website is restricted return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, statusLine, true); } else if (code == HttpStatus.SC_NOT_FOUND) { // no robots.txt provided return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, statusLine); } else if (code != HttpStatus.SC_OK) { // the robots.txt seems not to be deliverable return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, statusLine); } Header contentTypeHeader = getMethod.getResponseHeader("Content-Type"); if (contentTypeHeader != null && !contentTypeHeader.getValue().startsWith("text/plain")) { // the robots.txt seems not to be available return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_ERROR, "Wrong mimeType " + contentTypeHeader.getValue()); } inputStream = getMethod.getResponseBodyAsStream(); RobotsTxt robotsTxt = new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, statusLine); return this.parseRobotsTxt(robotsTxt, inputStream); } catch (IOException e) { long reloadInterval = RobotsTxt.RELOAD_INTERVAL_TEMP_ERROR; String status = e.getMessage(); if (e instanceof UnknownHostException) { reloadInterval = RobotsTxt.RELOAD_INTERVAL_ERROR; status = "Unknown host"; logger.info(String.format("Unknown host '%s'.", robotsURL.getHost())); } else if (e instanceof CircularRedirectException || e instanceof RedirectException || e instanceof InvalidRedirectLocationException) { reloadInterval = RobotsTxt.RELOAD_INTERVAL_ERROR; logger.info(String.format("Invalid redirection on host '%s'.", hostPort)); } else if (e instanceof SocketTimeoutException || e instanceof ConnectTimeoutException || e instanceof NoHttpResponseException) { logger.debug(String.format("TimeOut while loading robots.txt from host '%s'.", hostPort)); } else if (!(e instanceof ConnectException || e instanceof SocketException)) { logger.error("Exception while loading robots.txt from " + hostPort, e); } return new RobotsTxt(hostPort, reloadInterval, status); } catch (IllegalArgumentException e) { // occurs if redirected to an invalid URI, see https://bugs.pxl.li/view.php?id=172 // we treat it like a 404, see above logger.info(String.format("Invalid redirection URI on host '%s'.", hostPort)); return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, "Redirected to illegal URI"); } catch (IllegalStateException e) { // occurs if redirected to an URI with an invalid protocol, see https://bugs.pxl.li/view.php?id=169 // we treat it like a 404, see above logger.info(String.format("Invalid redirection URI on host '%s'.", hostPort)); return new RobotsTxt(hostPort, RobotsTxt.RELOAD_INTERVAL_DEFAULT, "Redirected to illegal URI"); } finally { if (inputStream != null) try { inputStream.close(); } catch (Exception e) { this.logger.error(e); } if (getMethod != null) getMethod.releaseConnection(); } }
From source file:org.paxle.se.provider.rsssearch.impl.RssSearchProvider.java
public void search(ISearchRequest searchRequest, List<IIndexerDocument> results) throws IOException, InterruptedException { String url = null;//from w w w.ja va 2s.c o m try { final ISearchProviderContext context = SearchProviderContext.getCurrentContext(); final String request = new RssSearchQueryFactor().transformToken(searchRequest.getSearchQuery()); final int maxCount = searchRequest.getMaxResultCount(); //creating a channel-builder ChannelBuilder builder = new ChannelBuilder(); // parsing the rss/atom feed HttpMethod hm = null; try { // opening an http connection url = new URL(String.format(feedURL, URLEncoder.encode(request, DEFAULT_CHARSET))).toExternalForm(); hm = new GetMethod(url); HttpClient hc = new HttpClient(); int status = hc.executeMethod(hm); if (status != 200) { this.logger.warn(String.format("Error while connecting to '%s'.\r\n\tServer-Status: %s", url, hm.getStatusLine())); return; } // parsing the rss/atom feed ChannelIF channel = FeedParser.parse(builder, hm.getResponseBodyAsStream()); // loop through all items Collection<ItemIF> items = channel.getItems(); Iterator<ItemIF> it = items.iterator(); int count = 0; while (it.hasNext() && count++ < maxCount) { ItemIF item = it.next(); IIndexerDocument indexerDoc = context.createDocument(); indexerDoc.set(IIndexerDocument.LOCATION, item.getLink().toString()); indexerDoc.set(IIndexerDocument.PROTOCOL, item.getLink().getProtocol()); String title = item.getTitle(); if (title != null && title.length() > 0) { indexerDoc.set(IIndexerDocument.TITLE, title); } String descr = item.getDescription(); if (descr != null && descr.length() > 0) { indexerDoc.set(IIndexerDocument.SUMMARY, descr); } String author = item.getCreator(); if (author != null && author.length() > 0) { indexerDoc.set(IIndexerDocument.AUTHOR, item.getCreator() == null ? "" : item.getCreator()); } Date lastMod = item.getDate(); if (lastMod != null) { indexerDoc.set(IIndexerDocument.LAST_MODIFIED, item.getDate()); } results.add(indexerDoc); } } catch (IOException e) { //do nothing, it just not worked (offline or rss-site problem) this.logger.warn(e); } finally { if (hm != null) hm.releaseConnection(); } } catch (Exception e) { this.logger.error(String.format("Unexpected '%s' while connecting to '%s'.", e.getClass().getName(), (url == null) ? this.feedURL : url)); } }
From source file:org.pentaho.pac.server.common.ThreadSafeHttpClient.java
/** * Execute the <param>method</param>, and return the server's response as a string * @param method the HttpMethod specifying the server URL and parameters to be * passed to the server.// w w w . jav a2s. c o m * @return a string containing the server's response * * @throws ProxyException if the attempt to communicate with the server fails, * if the attempt to read the response from the server fails, if the response * stream is unable to be converted into a String. */ private String executeMethod(HttpMethod method) throws ProxyException { InputStream responseStrm = null; try { int httpStatus = CLIENT.executeMethod(method); if (httpStatus != HttpStatus.SC_OK) { // If the response comes as unauthorized access we will throw a proxy exception explaining the reason and // what needs to be done to correct it if (httpStatus == HttpStatus.SC_UNAUTHORIZED) { throw new ProxyException( Messages.getErrorString("ThreadSafeHttpClient.ERROR_0003_AUTHORIZATION_FAILED")); } String status = method.getStatusLine().toString(); String uri = method.getURI().toString(); String errorMsg = Messages.getErrorString("ThreadSafeHttpClient.ERROR_0001_CLIENT_REQUEST_FAILED", //$NON-NLS-1$ uri, status); logger.error(errorMsg); throw new ProxyException(status); // TODO } responseStrm = method.getResponseBodyAsStream(); // trim() is necessary because some jsp's put \n\r at the beginning of // the returned text, and the xml processor chokes on \n\r at the beginning. String response = IOUtils.toString(responseStrm).trim(); return response; } catch (Exception e) { throw new ProxyException(e); } finally { method.releaseConnection(); } }
From source file:org.red5.server.net.rtmpt.RTMPTClientConnector.java
private void checkResponseCode(HttpMethod method) { if (method.getStatusCode() != HttpStatus.SC_OK) { try {/*from w ww .j a v a2s .c o m*/ String body = method.getResponseBodyAsString(); throw new RuntimeException( "Bad HTTP status returned, line: " + method.getStatusLine() + "; body: " + body); } catch (IOException e) { throw new RuntimeException("Bad HTTP status returned, line: " + method.getStatusLine() + "; in addition IOException occured on reading body", e); } } }
From source file:org.roosster.RoossterTestCase.java
/** * /* w w w . j av a2s. c o m*/ */ public void logMethodResponse(HttpMethod method) throws java.io.IOException { if (method == null) throw new IllegalArgumentException("FAILED: Tried to log 'null' method"); if (method.isRequestSent() == false) throw new IllegalArgumentException("FAILED: Tried to log 'not-sent' method"); System.out.println("++++++++++++++++++++++++ BEGIN HEADER LOGGING ++++++++++++++++++++++++"); StatusLine sline = method.getStatusLine(); System.out.println("\nStatusLine: " + sline.getHttpVersion() + " " + sline.getStatusCode() + " " + sline.getReasonPhrase()); System.out.println("\nHeader:\n"); Header[] headers = method.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i].toString()); } System.out.println("\nResponseBody:\n" + method.getResponseBodyAsString() + "\n\n"); System.out.println("++++++++++++++++++++++++ END HEADER LOGGING ++++++++++++++++++++++++\n\n"); }