HttpClient client = new HttpClient(); // truststore KeyStore trustStore = KeyStore.getInstance("JKS", "SUN"); trustStore.load(TestSupertype.class.getResourceAsStream("/client-truststore.jks"), "amber%".toCharArray()); String alg = KeyManagerFactory.getDefaultAlgorithm(); TrustManagerFactory fac = TrustManagerFactory.getInstance(alg); fac.init(trustStore); // keystore KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE"); keystore.load(X509Test.class.getResourceAsStream("/etomcat_client.p12"), "etomcat".toCharArray()); String keyAlg = KeyManagerFactory.getDefaultAlgorithm(); KeyManagerFactory keyFac = KeyManagerFactory.getInstance(keyAlg); keyFac.init(keystore, "etomcat".toCharArray()); // context SSLContext ctx = SSLContext.getInstance("TLS", "SunJSSE"); ctx.init(keyFac.getKeyManagers(), fac.getTrustManagers(), new SecureRandom()); SslContextedSecureProtocolSocketFactory secureProtocolSocketFactory = new SslContextedSecureProtocolSocketFactory(ctx); Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) secureProtocolSocketFactory, 8443)); // test get HttpMethod get = new GetMethod("https://127.0.0.1:8443/etomcat_x509"); client.executeMethod(get); // get response body and do what you need with it byte[] responseBody = get.getResponseBody();
public HttpComponent3Response(HttpMethod httpMethod) throws IOException { super(httpMethod.getResponseBody()); this.httpMethod = httpMethod; }
public void wipeCache(String type, String action, String name) throws IOException { String url = baseUrl + "/cache/" + type + "/" + name + "/" + action; HttpMethod request = new PutMethod(url); try { int code = client.executeMethod(request); String msg = Bytes.toString(request.getResponseBody()); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); } catch (HttpException ex) { throw new IOException(ex); } finally { request.releaseConnection(); } }
public String getKylinProperties() throws IOException { String url = baseUrl + "/admin/config"; HttpMethod request = new GetMethod(url); try { int code = client.executeMethod(request); String msg = Bytes.toString(request.getResponseBody()); JSONObject obj = new JSONObject(msg); msg = obj.getString("config"); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); return msg; } catch (JSONException e) { throw new IOException("Error when parsing json response from REST"); } finally { request.releaseConnection(); } }
public HttpComponent3Response(HttpMethod httpMethod) throws IOException { super(httpMethod.getResponseBody()); this.httpMethod = httpMethod; }
/** Returns the content of the response. */ public byte[] getResponseContent() throws java.io.IOException, HttpClientException { if (httpMethod == null) { throw new HttpClientException("Connection not yet established"); } return httpMethod.getResponseBody(); }
public byte[] getContentAsByteArray() { try { return method.getResponseBody(); } catch (IOException e) { return null; } }
public int getContentLength() { try { return method.getResponseBody().length; } catch (IOException e) { return 0; } }
protected Response request(HttpMethod method) throws IOException { final int statusCode; final String body; try { statusCode = client.executeMethod(method); body = new String(method.getResponseBody(), "utf-8"); logger.debug("Remote Control replied with '" + statusCode + " / '" + body + "'"); return new Response(statusCode, body); } finally { method.releaseConnection(); } }
protected Response request(HttpMethod method) throws IOException { final int statusCode; final String body; try { statusCode = client.executeMethod(method); body = new String(method.getResponseBody(), "utf-8"); logger.info("Remote Control replied with '" + statusCode + " / '" + body + "'"); return new Response(statusCode, body); } finally { method.releaseConnection(); } }
protected Response request(HttpMethod method) throws IOException { final int statusCode; final String body; try { statusCode = client.executeMethod(method); body = new String(method.getResponseBody(), "utf-8"); logger.debug("Remote Control replied with '" + statusCode + " / '" + body + "'"); return new Response(statusCode, body); } finally { method.releaseConnection(); } }
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());
public void wipeCache(String type, String action, String name) throws IOException { String url = baseUrl + "/cache/" + type + "/" + name + "/" + action; HttpMethod request = new PutMethod(url); try { int code = client.executeMethod(request); String msg = Bytes.toString(request.getResponseBody()); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); } catch (HttpException ex) { throw new IOException(ex); } finally { request.releaseConnection(); } }
public String getKylinProperties() throws IOException { String url = baseUrl + "/admin/config"; HttpMethod request = new GetMethod(url); try { int code = client.executeMethod(request); String msg = Bytes.toString(request.getResponseBody()); JSONObject obj = new JSONObject(msg); msg = obj.getString("config"); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); return msg; } catch (JSONException e) { throw new IOException("Error when parsing json response from REST"); } finally { request.releaseConnection(); } }
private int executeHTTPMethod(ClientRequestContext commandContext, HttpMethod method) throws Exception { // Push context HttpURLConnection conn = (HttpURLConnection)new URL( driverURL + "?test=" + testName + "&step=" + commandContext.getRequestCount() ).openConnection(); byte[] payload = IOTools.serialize(commandContext.getPayload()); conn.setRequestProperty("Content-Type", "application/octet-stream"); conn.setRequestProperty("Content-Length", "" + payload.length); conn.setDoOutput(true); conn.connect(); OutputStream out = conn.getOutputStream(); IOTools.copy(new ByteArrayInputStream(payload), out); out.close(); IOTools.getBytes(conn.getInputStream()); // log.info("# Invoking test case over http " + method.getURI()); int status = client.executeMethod(method); // Force to read the response body before we close the connection // otherwise the content will be lost method.getResponseBody(); // return status; } }
@Override public Document getDocument(Document document, DocumentFactory factory) throws IOException { HttpMethod request = new GetMethod(baseURL); request.addRequestHeader("Authorization", String.format("Bearer %s", bearerToken)); //TODO request.setPath(path + document.getId()); int status = client.executeMethod(request); if(status == 200) { JsonNode json = mapper.readValue(request.getResponseBody(), JsonNode.class); for(FieldDescriptor descriptor : factory.listFields()) { try { Object value = getValue(json, descriptor); if(value != null) { document.setValue(descriptor, value); } else LOG.warn("No data found for id {}", document.getId()); } catch (IOException e) { LOG.warn("Cannot use data for id {}: {}", document.getId(), e.getMessage()); } } return document; } else throw new IOException(request.getStatusText()); }
/** * Turns out {@link #testDoubleCompression()} was insufficient to properly test the case, * as HttpURLConnection appears to ignores the content-length header and read the response to the end. */ public void testDoubleCompression2() throws Exception { for (String endpoint : Arrays.asList("autoZip","ownZip")) { HttpClient hc = new HttpClient(); HttpMethod m = new GetMethod(this.url + "/"+endpoint); m.setRequestHeader("Accept-Encoding", "gzip"); assertEquals(200, hc.executeMethod(m)); byte[] data = m.getResponseBody(); data = IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(data))); assertEquals(new String(data), CONTENT); } }
Object read() throws IOException { HttpMethod get = new GetMethod(getEndpoint(this.cacheName) + "/" + this.key); if (this.accept != null) { get.setRequestHeader(ACCEPT, this.accept.toString()); } if (keyContentType != null) { get.setRequestHeader("Key-Content-Type", this.keyContentType); } restClient.executeMethod(get); assertEquals(get.getStatusCode(), HttpStatus.SC_OK); return get.getResponseBody(); } }
public void testByteArrayHotRodEmbeddedPutRestGet() throws Exception { final String key1 = "14"; final String key2 = "15"; // 1. Put with Hot Rod RemoteCache<String, Object> remote = cacheFactory.getHotRodCache(); assertEquals(null, remote.withFlags(Flag.FORCE_RETURN_VALUE).put(key1, "v1".getBytes())); // 2. Put with Embedded assertEquals(null, cacheFactory.getEmbeddedCache().put(key2, "v2".getBytes())); // 3. Get with REST key1 HttpMethod getHotRodValue = new GetMethod(cacheFactory.getRestUrl() + "/" + key1); getHotRodValue.setRequestHeader("Accept", "text/plain"); cacheFactory.getRestClient().executeMethod(getHotRodValue); assertEquals(getHotRodValue.getStatusText(), HttpStatus.SC_OK, getHotRodValue.getStatusCode()); assertEquals("text/plain", getHotRodValue.getResponseHeader("Content-Type").getValue()); assertArrayEquals("v1".getBytes(), getHotRodValue.getResponseBody()); // 4. Get with REST key2 HttpMethod getEmbeddedValue = new GetMethod(cacheFactory.getRestUrl() + "/" + key2); getEmbeddedValue.setRequestHeader("Accept", "text/plain"); cacheFactory.getRestClient().executeMethod(getEmbeddedValue); assertEquals(getEmbeddedValue.getStatusText(), HttpStatus.SC_OK, getEmbeddedValue.getStatusCode()); assertEquals("text/plain", getEmbeddedValue.getResponseHeader("Content-Type").getValue()); assertArrayEquals("v2".getBytes(), getEmbeddedValue.getResponseBody()); }
private void send(HttpMessage msg, boolean isFollowRedirect, HttpMethodParams params) throws IOException { HttpMethod method = null; HttpResponseHeader resHeader = null; try { method = runMethod(msg, isFollowRedirect, params); // successfully executed; resHeader = HttpMethodHelper.getHttpResponseHeader(method); resHeader.setHeader(HttpHeader.TRANSFER_ENCODING, null); // replaceAll("Transfer-Encoding: chunked\r\n", // ""); msg.setResponseHeader(resHeader); msg.getResponseBody().setCharset(resHeader.getCharset()); msg.getResponseBody().setLength(0); // ZAP: Do not read response body for Server-Sent Events stream // ZAP: Moreover do not set content length to zero if (!msg.isEventStream()) { msg.getResponseBody().append(method.getResponseBody()); } msg.setResponseFromTargetHost(true); // ZAP: set method to retrieve upgraded channel later if (method instanceof ZapGetMethod) { msg.setUserObject(method); } } finally { if (method != null) { method.releaseConnection(); } } }