List of usage examples for org.apache.http.message BasicHttpResponse BasicHttpResponse
public BasicHttpResponse(StatusLine statusLine)
From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java
@Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); HashMap<String, String> map = new HashMap<String, String>(); // chenbo add gzip support,new user-agent if (request.isShouldGzip()) { map.put(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); }//from w w w . j a va 2s . co m map.put(USER_AGENT, mUserAgent); // end map.putAll(request.getHeaders()); map.putAll(additionalHeaders); if (mUrlRewriter != null) { String rewritten = mUrlRewriter.rewriteUrl(url); if (rewritten == null) { throw new IOException("URL blocked by rewriter: " + url); } url = rewritten; } URL parsedUrl = new URL(url); HttpURLConnection connection = openConnection(parsedUrl, request); for (String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } // if (request instanceof MultiPartRequest) { // setConnectionParametersForMultipartRequest(connection, request); // } else { // } setConnectionParametersForRequest(connection, request); // Initialize HttpResponse with data from the HttpURLConnection. ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); int responseCode = connection.getResponseCode(); if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could not be retrieved. // Signal to the caller that something was wrong with the connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } if (ENCODING_GZIP.equalsIgnoreCase(connection.getContentEncoding())) { response.setEntity(new InflatingEntity(response.getEntity())); } return response; }
From source file:com.hackerati.android.user_sdk.volley.HHurlStack.java
@Override public HttpResponse performRequest(final Request<?> request, final Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); final HashMap<String, String> map = new HashMap<String, String>(); map.putAll(request.getHeaders());/* ww w. java 2 s .c om*/ map.putAll(additionalHeaders); if (mUrlRewriter != null) { final String rewritten = mUrlRewriter.rewriteUrl(url); if (rewritten == null) { throw new IOException("URL blocked by rewriter: " + url); } url = rewritten; } final URL parsedUrl = new URL(url); final HttpURLConnection connection = openConnection(parsedUrl, request); for (final String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } setConnectionParametersForRequest(connection, request); // Initialize HttpResponse with data from the HttpURLConnection. final ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); int responseCode; try { // Will throw IOException if server responds with 401. responseCode = connection.getResponseCode(); } catch (final IOException e) { // Will return 401, because now connection has the correct internal // state. responseCode = connection.getResponseCode(); } if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could // not be retrieved. // Signal to the caller that something was wrong with the // connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } final StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage()); final BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (final Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { final Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } return response; }
From source file:org.codice.ddf.commands.solr.TestBackupCommand.java
private HttpResponse prepareResponse(int statusCode, String responseBody) { HttpResponse httpResponse = new BasicHttpResponse( new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), statusCode, "")); httpResponse.setStatusCode(statusCode); try {/*w w w. j av a2 s .com*/ httpResponse.setEntity(new StringEntity(responseBody)); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } return httpResponse; }
From source file:org.esigate.http.HttpClientRequestExecutorTest.java
private HttpResponse createMockGzippedResponse(String content) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); byte[] uncompressedBytes = content.getBytes(); gzos.write(uncompressedBytes, 0, uncompressedBytes.length); gzos.close();/* w w w.j av a2 s . co m*/ byte[] compressedBytes = baos.toByteArray(); ByteArrayEntity httpEntity = new ByteArrayEntity(compressedBytes); httpEntity.setContentType("text/html; charset=ISO-8859-1"); httpEntity.setContentEncoding("gzip"); StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("p", 1, 2), HttpStatus.SC_OK, "OK"); BasicHttpResponse httpResponse = new BasicHttpResponse(statusLine); httpResponse.addHeader("Content-type", "text/html; charset=ISO-8859-1"); httpResponse.addHeader("Content-encoding", "gzip"); httpResponse.setEntity(httpEntity); return httpResponse; }
From source file:com.android.volley.toolbox.AuthenticationChallengesProofHurlStack.java
@Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); HashMap<String, String> map = new HashMap<String, String>(); map.putAll(request.getHeaders());// w ww . java2 s .c o m map.putAll(additionalHeaders); if (mUrlRewriter != null) { String rewritten = mUrlRewriter.rewriteUrl(url); if (rewritten == null) { throw new IOException("URL blocked by rewriter: " + url); } url = rewritten; } URL parsedUrl = new URL(url); HttpURLConnection connection = openConnection(parsedUrl, request); for (String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } setConnectionParametersForRequest(connection, request); // Initialize HttpResponse with data from the HttpURLConnection. ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); /************************/ /****** WORKAROUND ******/ int responseCode; try { // Will throw IOException if server responds with 401. responseCode = connection.getResponseCode(); } catch (IOException e) { // You can get the response code after an exception if you call .getResponseCode() // a second time on the connection object. This is because the first time you // call .getResponseCode() an internal state is set that enables .getResponseCode() // to return without throwing an exception. responseCode = connection.getResponseCode(); } /************************/ if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could not be retrieved. // Signal to the caller that something was wrong with the connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } return response; }
From source file:com.baseproject.volley.toolbox.HurlStack.java
@Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); HashMap<String, String> map = new HashMap<String, String>(); map.putAll(request.getHeaders());// w w w .j a v a 2s . co m map.putAll(additionalHeaders); if (mUrlRewriter != null) { String rewritten = mUrlRewriter.rewriteUrl(url); if (rewritten == null) { throw new IOException(Util.buildHttpErrorMsg("failed", -1, "URL blocked by rewriter: " + url)); } url = rewritten; } URL parsedUrl = new URL(url); HttpURLConnection connection = openConnection(parsedUrl, request); for (String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } setConnectionParametersForRequest(connection, request); // Initialize HttpResponse with data from the HttpURLConnection. ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); int responseCode = connection.getResponseCode(); // if (responseCode == -1) { // // -1 is returned by getResponseCode() if the response code could not be retrieved. // // Signal to the caller that something was wrong with the connection. // throw new IOException("Could not retrieve response code from HttpUrlConnection."); // } if (responseCode != HttpStatus.SC_NOT_MODIFIED && responseCode != HttpURLConnection.HTTP_OK) { InputStream in = null; try { in = connection.getErrorStream(); } catch (Exception e) { e.printStackTrace(); } if (in == null) { throw new IOException(Util.buildHttpErrorMsg("failed", responseCode, "unknown")); } else { throw new IOException(Util.convertStreamToString(in)); } } StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { if (header.getKey().equals("Set-Cookie")) { List<String> cookieValue = header.getValue(); for (String c : cookieValue) { Header h = new BasicHeader(header.getKey(), c); response.addHeader(h); } } else { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } } return response; }
From source file:org.elasticsearch.client.RequestLoggerTests.java
public void testTraceResponse() throws IOException { ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); int statusCode = randomIntBetween(200, 599); String reasonPhrase = "REASON"; BasicStatusLine statusLine = new BasicStatusLine(protocolVersion, statusCode, reasonPhrase); String expected = "# " + statusLine.toString(); BasicHttpResponse httpResponse = new BasicHttpResponse(statusLine); int numHeaders = randomIntBetween(0, 3); for (int i = 0; i < numHeaders; i++) { httpResponse.setHeader("header" + i, "value"); expected += "\n# header" + i + ": value"; }/* w w w.j a va 2s . c o m*/ expected += "\n#"; boolean hasBody = getRandom().nextBoolean(); String responseBody = "{\n \"field\": \"value\"\n}"; if (hasBody) { expected += "\n# {"; expected += "\n# \"field\": \"value\""; expected += "\n# }"; HttpEntity entity; switch (randomIntBetween(0, 2)) { case 0: entity = new StringEntity(responseBody, ContentType.APPLICATION_JSON); break; case 1: //test a non repeatable entity entity = new InputStreamEntity( new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)), ContentType.APPLICATION_JSON); break; case 2: // Evil entity without a charset entity = new StringEntity(responseBody, ContentType.create("application/json", (Charset) null)); break; default: throw new UnsupportedOperationException(); } httpResponse.setEntity(entity); } String traceResponse = RequestLogger.buildTraceResponse(httpResponse); assertThat(traceResponse, equalTo(expected)); if (hasBody) { //check that the body is still readable as most entities are not repeatable String body = EntityUtils.toString(httpResponse.getEntity(), StandardCharsets.UTF_8); assertThat(body, equalTo(responseBody)); } }
From source file:common.net.volley.toolbox.HurlStack.java
@Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); stethoManager = new StethoURLConnectionManager(url); HashMap<String, String> map = new HashMap<String, String>(); map.putAll(request.getHeaders());//from ww w. ja v a 2s.c o m map.putAll(additionalHeaders); if (mUrlRewriter != null) { String rewritten = mUrlRewriter.rewriteUrl(url); if (rewritten == null) { throw new IOException("URL blocked by rewriter: " + url); } url = rewritten; } URL parsedUrl = new URL(url); HttpURLConnection connection = openConnection(parsedUrl, request); requestDecompression(connection); for (String headerName : map.keySet()) { connection.addRequestProperty(headerName, map.get(headerName)); } setConnectionParametersForRequest(connection, request); // Initialize HttpResponse with data from the HttpURLConnection. ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); int responseCode = connection.getResponseCode(); if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could not be retrieved. // Signal to the caller that something was wrong with the connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage()); stethoManager.postConnect(); BasicHttpResponse response = new BasicHttpResponse(responseStatus); response.setEntity(entityFromConnection(connection)); for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } return response; }
From source file:com.iflytek.android.framework.volley.toolbox.HurlStack.java
@Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { String url = request.getUrl(); HashMap<String, String> map = new HashMap<String, String>(); map.putAll(request.getHeaders());//from w ww .j a va 2 s.co m map.putAll(additionalHeaders); if (mUrlRewriter != null) { String rewritten = mUrlRewriter.rewriteUrl(url); if (rewritten == null) { throw new IOException("URL blocked by rewriter: " + url); } url = rewritten; } URL parsedUrl = new URL(url); HttpURLConnection connection = openConnection(parsedUrl, request); for (String headerName : map.keySet()) { VolleyLog.e("======4:" + headerName + ";" + map.get(headerName)); connection.addRequestProperty(headerName, map.get(headerName)); } setConnectionParametersForRequest(connection, request); // Initialize HttpResponse with data from the HttpURLConnection. ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1); int responseCode = connection.getResponseCode(); if (responseCode == -1) { // -1 is returned by getResponseCode() if the response code could // not be retrieved. // Signal to the caller that something was wrong with the // connection. throw new IOException("Could not retrieve response code from HttpUrlConnection."); } StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage()); BasicHttpResponse response = new BasicHttpResponse(responseStatus); if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) { response.setEntity(entityFromConnection(connection)); } for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) { if (header.getKey() != null) { Header h = new BasicHeader(header.getKey(), header.getValue().get(0)); response.addHeader(h); } } return response; }
From source file:dk.slott.super_volley.stacks.ExtHttpClientStack.java
private org.apache.http.HttpResponse convertResponseNewToOld(HttpResponse resp) throws IllegalStateException, IOException { final ProtocolVersion protocolVersion = new ProtocolVersion(resp.getProtocolVersion().getProtocol(), resp.getProtocolVersion().getMajor(), resp.getProtocolVersion().getMinor()); final StatusLine responseStatus = new BasicStatusLine(protocolVersion, resp.getStatusLine().getStatusCode(), resp.getStatusLine().getReasonPhrase()); final BasicHttpResponse response = new BasicHttpResponse(responseStatus); final org.apache.http.HttpEntity ent = convertEntityNewToOld(resp.getEntity()); response.setEntity(ent);/*from w ww.j a v a2 s . co m*/ for (Header h : resp.getAllHeaders()) { org.apache.http.Header header = convertheaderNewToOld(h); response.addHeader(header); } return response; }