/** * Return the header field at the specified position * @param position The position * @return The header field. * @see java.net.HttpURLConnection#getHeaderField(int) * @see org.apache.commons.httpclient.HttpMethod#getResponseHeaders() */ public String getHeaderField(int position) { LOG.trace("enter HttpURLConnection.getHeaderField(int)"); // Note: HttpClient does not consider the returned Status Line as // a response header. However, getHeaderField(0) is supposed to // return the status line. Hence the special case below ... if (position == 0) { return this.method.getStatusLine().toString(); } // Note: HttpClient does not currently keep headers in the same order // that they are read from the HTTP server. Header[] headers = this.method.getResponseHeaders(); if (position < 0 || position > headers.length) { return null; } return headers[position - 1].getValue(); }
logger.debug("Method failed: {}", method.getStatusLine());
public String getReasonPhrase() throws Exception { return httpMethod.getStatusLine().getReasonPhrase(); }
public void mockConnection(String responseBody) throws Exception { InputStream is = new ByteArrayInputStream(responseBody.getBytes()); StatusLine statusLine = new StatusLine("HTTP/1.1 200 OK"); this.httpMethodMock = mock(HttpMethod.class); when(httpMethodMock.getStatusLine()).thenReturn(statusLine); when(httpMethodMock.getStatusCode()).thenReturn(200); when(httpMethodMock.getResponseBodyAsStream()).thenReturn(is); } }
private static String getHttpResponseHeaderAsString(HttpMethod httpMethod) { StringBuilder sb = new StringBuilder(200); String name = null; 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(); }
HttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://www.google.com/search?q=" + query); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } String response = new String(method.getResponseBody());
/** * Return the header field at the specified position * @param position The position * @return The header field. * @see java.net.HttpURLConnection#getHeaderField(int) * @see org.apache.commons.httpclient.HttpMethod#getResponseHeaders() */ public String getHeaderField(int position) { LOG.trace("enter HttpURLConnection.getHeaderField(int)"); // Note: HttpClient does not consider the returned Status Line as // a response header. However, getHeaderField(0) is supposed to // return the status line. Hence the special case below ... if (position == 0) { return this.method.getStatusLine().toString(); } // Note: HttpClient does not currently keep headers in the same order // that they are read from the HTTP server. Header[] headers = this.method.getResponseHeaders(); if (position < 0 || position > headers.length) { return null; } return headers[position - 1].getValue(); }
/** * Return the header field at the specified position * @param position The position * @return The header field. * @see java.net.HttpURLConnection#getHeaderField(int) * @see org.apache.commons.httpclient.HttpMethod#getResponseHeaders() */ public String getHeaderField(int position) { LOG.trace("enter HttpURLConnection.getHeaderField(int)"); // Note: HttpClient does not consider the returned Status Line as // a response header. However, getHeaderField(0) is supposed to // return the status line. Hence the special case below ... if (position == 0) { return this.method.getStatusLine().toString(); } // Note: HttpClient does not currently keep headers in the same order // that they are read from the HTTP server. Header[] headers = this.method.getResponseHeaders(); if (position < 0 || position > headers.length) { return null; } return headers[position - 1].getValue(); }
/** * Return the header field at the specified position * @param position The position * @return The header field. * @see java.net.HttpURLConnection#getHeaderField(int) * @see org.apache.commons.httpclient.HttpMethod#getResponseHeaders() */ public String getHeaderField(int position) { LOG.trace("enter HttpURLConnection.getHeaderField(int)"); // Note: HttpClient does not consider the returned Status Line as // a response header. However, getHeaderField(0) is supposed to // return the status line. Hence the special case below ... if (position == 0) { return this.method.getStatusLine().toString(); } // Note: HttpClient does not currently keep headers in the same order // that they are read from the HTTP server. Header[] headers = this.method.getResponseHeaders(); if (position < 0 || position > headers.length) { return null; } return headers[position - 1].getValue(); }
/** * Return the header field at the specified position * @param position The position * @return The header field. * @see java.net.HttpURLConnection#getHeaderField(int) * @see org.apache.commons.httpclient.HttpMethod#getResponseHeaders() */ public String getHeaderField(int position) { LOG.trace("enter HttpURLConnection.getHeaderField(int)"); // Note: HttpClient does not consider the returned Status Line as // a response header. However, getHeaderField(0) is supposed to // return the status line. Hence the special case below ... if (position == 0) { return this.method.getStatusLine().toString(); } // Note: HttpClient does not currently keep headers in the same order // that they are read from the HTTP server. Header[] headers = this.method.getResponseHeaders(); if (position < 0 || position > headers.length) { return null; } return headers[position - 1].getValue(); }
throw new CatalogException("HTTP method failed: " + method.getStatusLine());
log.info("setupResponseHeaders"); log.info("status text: " + httpMethod.getStatusText()); log.info("status line: " + httpMethod.getStatusLine());
public InputStream sendData(RestRequest request) throws RestException, IOException { if (!open) { throw new IllegalStateException("the session is not opened"); } HttpMethod gm = doRequest(request); StatusLine sl = gm.getStatusLine(); int statusCode = sl.getStatusCode(); if (log.isDebugEnabled()) { log.debug("status code " + statusCode + " for " + gm.getPath()); } if (statusCode != HttpStatus.SC_OK) { log.error(gm.getResponseBodyAsString()); releaseConnection(getConnection()); throw new RestException( "Got error code <" + statusCode + ":" + sl.getReasonPhrase() + "> on " + gm.getPath()); } InputStream stream = gm.getResponseBodyAsStream(); return stream; }
headerString(this.m_httpMethod.getRequestHeaders())+ "\nResponse:\n" + this.m_httpMethod.getStatusLine() + "\n" + headerString(this.m_httpMethod.getResponseHeaders()));
log.info("setupResponseHeaders"); log.info("status text: " + httpMethod.getStatusText()); log.info("status line: " + httpMethod.getStatusLine());
LOG.error("Method failed: {}", getMethod().getStatusLine()); throw new InvokerException("Method failed: " + getMethod().getStatusLine());
public InputStream askData(RestRequest request) throws RestException, IOException { if (!open) { throw new IllegalStateException("the session is not opened"); } HttpMethod gm; gm = doRequest(request); StatusLine sl = gm.getStatusLine(); int statusCode = sl.getStatusCode(); if (log.isDebugEnabled()) { log.debug("status code " + statusCode + " for " + gm.getPath()); } if (statusCode == HttpStatus.SC_NOT_FOUND) { throw new RestDataNotFoundException( "could not retreave some datas : " + gm.getResponseBodyAsString()); } if (statusCode != HttpStatus.SC_OK) { log.error("Error = " + gm.getResponseBodyAsString()); releaseConnection(getConnection()); throw new RestException( "Got error code <" + statusCode + ":" + sl.getReasonPhrase() + "> on " + gm.getPath()); } InputStream stream = gm.getResponseBodyAsStream(); return stream; }
LOG.error("Method failed: " + mMethod.getStatusLine()); throw new InvokerException("Method failed: " + mMethod.getStatusLine());
String statusText = method.getStatusLine() != null ? method.getStatusLine().getReasonPhrase() : null; Map<String, String> headers = extractResponseHeaders(method.getResponseHeaders());
/** * @param queryMap * @return * @throws Exception */ public String invoke(Map<String, String> queryMap) throws Exception { // set query parameters getMethod().setQueryString(getQueryString(queryMap)); // check if we have a payload to send if (getPayload() != null) { RequestEntity entity = new StringRequestEntity(getPayload(), "text/xml", "UTF-8"); ((PostMethod)getMethod()).setRequestEntity(entity); } String lResponse = null; // Execute request try { int statusCode = getClient().executeMethod(getMethod()); lResponse = getMethod().getResponseBodyAsString(); if (statusCode != HttpStatus.SC_OK) { LOG.error("Method failed: {}", getMethod().getStatusLine()); throw new InvokerException("Method failed: " + getMethod().getStatusLine()); } } finally { LOG.debug("Got the following response: {}", lResponse); // Release current connection to the connection pool once you are done getMethod().releaseConnection(); } return lResponse; }