List of usage examples for org.apache.http.message BasicHttpRequest BasicHttpRequest
public BasicHttpRequest(String str, String str2)
From source file:com.alexjalg.gson_google.EjecutarHttpClient.java
public static void main(String[] args) throws Exception { HttpProcessor httpproc = HttpProcessorBuilder.create().add(new RequestContent()) .add(new RequestTargetHost()).add(new RequestConnControl()).add(new RequestUserAgent("Test/1.1")) .add(new RequestExpectContinue(true)).build(); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpCoreContext coreContext = HttpCoreContext.create(); HttpHost host = new HttpHost("jsonplaceholder.typicode.com", 80); coreContext.setTargetHost(host);//from ww w . j a va 2 s . co m DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024); ConnectionReuseStrategy connStrategy = DefaultConnectionReuseStrategy.INSTANCE; try { String[] targets = { "/posts" }; for (int i = 0; i < targets.length; i++) { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket); } BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); httpexecutor.preProcess(request, httpproc, coreContext); HttpResponse response = httpexecutor.execute(request, conn, coreContext); httpexecutor.postProcess(response, httpproc, coreContext); System.out.println("<< Response: " + response.getStatusLine()); // System.out.println(EntityUtils.toString(response.getEntity())); String json = EntityUtils.toString(response.getEntity()); ArrayList<Posts> listPosts = new ArrayList<Posts>(); Type listType = new TypeToken<ArrayList<Posts>>() { }.getType(); listPosts = new Gson().fromJson(json, listType); for (Posts post : listPosts) { System.out.println(""); System.out.println(post.getUserId()); System.out.println(post.getId()); System.out.println(post.getTitle()); System.out.println(post.getBody()); } System.out.println("=============="); if (!connStrategy.keepAlive(response, coreContext)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } } finally { conn.close(); } }
From source file:cn.heroes.ud.protocol.ElementalHttpGet.java
public static void main(String[] args) throws Exception { HttpProcessor httpproc = HttpProcessorBuilder.create().add(new RequestContent()) .add(new RequestTargetHost()).add(new RequestConnControl()).add(new RequestUserAgent("Test/1.1")) .add(new RequestExpectContinue(true)).build(); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpCoreContext coreContext = HttpCoreContext.create(); HttpHost host = new HttpHost("localhost", 8080); coreContext.setTargetHost(host);/*from w ww .ja v a 2s . c o m*/ DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024); ConnectionReuseStrategy connStrategy = DefaultConnectionReuseStrategy.INSTANCE; try { String[] targets = { "/", "/servlets-examples/servlet/RequestInfoExample", "/somewhere%20in%20pampa" }; for (int i = 0; i < targets.length; i++) { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket); } BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); httpexecutor.preProcess(request, httpproc, coreContext); HttpResponse response = httpexecutor.execute(request, conn, coreContext); httpexecutor.postProcess(response, httpproc, coreContext); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("=============="); if (!connStrategy.keepAlive(response, coreContext)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } } finally { conn.close(); } }
From source file:org.atmosphere.tictactoe42a9x.ElementalHttpGet.java
public static void main(String[] args) throws Exception { HttpParams params = new SyncBasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1"); HttpProtocolParams.setUseExpectContinue(params, true); HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { // Required protocol interceptors new RequestContent(), new RequestTargetHost(), // Recommended protocol interceptors new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue() }); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); HttpHost host = new HttpHost("localhost", 8080); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); try {//from w w w. j a v a2 s . c o m String[] targets = { "/", "/servlets-examples/servlet/RequestInfoExample", "/somewhere%20in%20pampa" }; for (int i = 0; i < targets.length; i++) { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, params); } BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); request.setParams(params); httpexecutor.preProcess(request, httpproc, context); HttpResponse response = httpexecutor.execute(request, conn, context); response.setParams(params); httpexecutor.postProcess(response, httpproc, context); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } } finally { conn.close(); } }
From source file:net.ElementalHttpGet.java
public static void main(String[] args) throws Exception { HttpParams params = new SyncBasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1"); HttpProtocolParams.setUseExpectContinue(params, true); HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { // Required protocol interceptors new RequestContent(), new RequestTargetHost(), // Recommended protocol interceptors new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue() }); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); HttpHost host = new HttpHost("www.baidu.com", 80); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); try {/* www . ja v a 2 s. c om*/ String[] targets = { "/", "/servlets-examples/servlet/RequestInfoExample?a=1&b=2", "/somewhere%20in%20pampa" }; for (int i = 0; i < targets.length; i++) { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, params); } BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); request.setParams(params); httpexecutor.preProcess(request, httpproc, context); HttpResponse response = httpexecutor.execute(request, conn, context); response.setParams(params); httpexecutor.postProcess(response, httpproc, context); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } } finally { conn.close(); } }
From source file:localhost.potlatchserver.AutoGrading.java
public static void main(String[] args) throws Exception { // Ensure that this application is run within the correct working directory File f = new File("./src/main/java/localhost/potlatchserver/Application.java"); if (!f.exists()) { System.out.println(WordUtils.wrap("You must run the AutoGrading application from the root of the " + "project directory containing src/main/java. If you right-click->Run As->Java Application " + "in Eclipse, it will automatically use the correct classpath and working directory " + "(assuming that you have Gradle and the project setup correctly).", 80)); System.exit(1);//from w ww. j av a2s .c o m } // Ensure that the server is running and accessible on port 8443 try { HttpClient client = UnsafeHttpsClient.createUnsafeClient(); HttpResponse response = client.execute(new HttpHost("127.0.0.1", 8443), new BasicHttpRequest("GET", "/")); response.getStatusLine(); } catch (NoHttpResponseException e) { // The server may have returned some JSON object } catch (Exception e) { System.out.println(WordUtils.wrap( "Unable to connect to your server on https://localhost:8443. Are you sure the server is running? " + "In order to run the autograder, you must first launch your application " + "by right-clicking on the Application class in Eclipse, and" + "choosing Run As->Java Application. If you have already done this, make sure that" + " you can access your server by opening the https://localhost:8443 url in a browser. " + "If you can't access the server in a browser, it probably indicates you have a firewall " + "or some other issue that is blocking access to port 8080 on localhost.", 80)); System.exit(1); } HandinUtil.generateHandinPackage("Asgn2", new File("./"), InternalAutoGradingTest.class); }
From source file:org.magnum.mobilecloud.video.AutoGrading.java
public static void main(String[] args) throws Exception { // Ensure that this application is run within the correct working directory File f = new File("./src/main/java/org/magnum/mobilecloud/video/Application.java"); if (!f.exists()) { System.out.println(WordUtils.wrap("You must run the AutoGrading application from the root of the " + "project directory containing src/main/java. If you right-click->Run As->Java Application " + "in Eclipse, it will automatically use the correct classpath and working directory " + "(assuming that you have Gradle and the project setup correctly).", 80)); System.exit(1);//from w w w. j a va 2 s .c o m } // Ensure that the server is running and accessible on port 8443 try { HttpClient client = UnsafeHttpsClient.createUnsafeClient(); HttpResponse response = client.execute(new HttpHost("127.0.0.1", 8443), new BasicHttpRequest("GET", "/")); response.getStatusLine(); } catch (NoHttpResponseException e) { // The server may have returned some JSON object } catch (Exception e) { System.out.println(WordUtils.wrap( "Unable to connect to your server on https://localhost:8443. Are you sure the server is running? " + "In order to run the autograder, you must first launch your application " + "by right-clicking on the Application class in Eclipse, and" + "choosing Run As->Java Application. If you have already done this, make sure that" + " you can access your server by opening the https://localhost:8443 url in a browser. " + "If you can't access the server in a browser, it probably indicates you have a firewall " + "or some other issue that is blocking access to port 8080 on localhost.", 80)); System.exit(1); } HandinUtil.generateHandinPackage("Asgn2", new File("./"), InternalAutoGradingTest.class); }
From source file:com.yulore.demo.NHttpClient.java
public static void main(String[] args) throws Exception { // Create HTTP protocol processing chain HttpProcessor httpproc = HttpProcessorBuilder.create() // Use standard client-side protocol interceptors .add(new RequestContent()).add(new RequestTargetHost()).add(new RequestConnControl()) .add(new RequestUserAgent("Test/1.1")).add(new RequestExpectContinue(true)).build(); // Create client-side HTTP protocol handler HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor(); // Create client-side I/O event dispatch final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, ConnectionConfig.DEFAULT);//from w ww . j a va2 s . co m // Create client-side I/O reactor final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); // Create HTTP connection pool BasicNIOConnPool pool = new BasicNIOConnPool(ioReactor, ConnectionConfig.DEFAULT); // Limit total number of connections to just two pool.setDefaultMaxPerRoute(2); pool.setMaxTotal(2); // Run the I/O reactor in a separate thread Thread t = new Thread(new Runnable() { public void run() { try { // Ready to go! ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { System.err.println("Interrupted"); } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); } System.out.println("Shutdown"); } }); // Start the client thread t.start(); // Create HTTP requester HttpAsyncRequester requester = new HttpAsyncRequester(httpproc); // Execute HTTP GETs to the following hosts and HttpHost[] targets = new HttpHost[] { new HttpHost("www.apache.org", 80, "http"), new HttpHost("www.verisign.com", 443, "https"), new HttpHost("www.google.com", 80, "http") }; final CountDownLatch latch = new CountDownLatch(targets.length); for (final HttpHost target : targets) { BasicHttpRequest request = new BasicHttpRequest("GET", "/"); HttpCoreContext coreContext = HttpCoreContext.create(); requester.execute(new BasicAsyncRequestProducer(target, request), new BasicAsyncResponseConsumer(), pool, coreContext, // Handle HTTP response from a callback new FutureCallback<HttpResponse>() { public void completed(final HttpResponse response) { latch.countDown(); System.out.println(target + "->" + response.getStatusLine()); } public void failed(final Exception ex) { latch.countDown(); System.out.println(target + "->" + ex); } public void cancelled() { latch.countDown(); System.out.println(target + " cancelled"); } }); } latch.await(); System.out.println("Shutting down I/O reactor"); ioReactor.shutdown(); System.out.println("Done"); }
From source file:io.aos.protocol.http.httpcommon.ElementalHttpGet.java
public static void main(String... args) throws Exception { HttpParams params = new SyncBasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1"); HttpProtocolParams.setUseExpectContinue(params, true); HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { // Required protocol interceptors new RequestContent(), new RequestTargetHost(), // Recommended protocol interceptors new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue() }); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); HttpHost host = new HttpHost("localhost", 8080); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); try {// w ww . j a v a 2 s. c o m String[] targets = { "/", "/servlets-examples/servlet/RequestInfoExample", "/somewhere%20in%20pampa" }; for (int i = 0; i < targets.length; i++) { if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket, params); } BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); request.setParams(params); httpexecutor.preProcess(request, httpproc, context); HttpResponse response = httpexecutor.execute(request, conn, context); response.setParams(params); httpexecutor.postProcess(response, httpproc, context); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("=============="); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { System.out.println("Connection kept alive..."); } } } finally { conn.close(); } }
From source file:framework.httpclient.nio.NHttpClient.java
public static void main(String[] args) throws Exception { // Create HTTP protocol processing chain HttpProcessor httpproc = HttpProcessorBuilder.create() // Use standard client-side protocol interceptors .add(new RequestContent()).add(new RequestTargetHost()).add(new RequestConnControl()) .add(new RequestUserAgent("LinkedHashSetVsTreeSet/1.1")).add(new RequestExpectContinue(true)) .build();/* w ww.ja v a2 s. c om*/ // Create client-side HTTP protocol handler HttpAsyncRequestExecutor protocolHandler = new HttpAsyncRequestExecutor(); // Create client-side I/O event dispatch // IO final IOEventDispatch ioEventDispatch = new DefaultHttpClientIODispatch(protocolHandler, ConnectionConfig.DEFAULT); // Create client-side I/O reactor // IO reactor final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); // Create HTTP connection pool // HTTP BasicNIOConnPool pool = new BasicNIOConnPool(ioReactor, ConnectionConfig.DEFAULT); // Limit total number of connections to just two pool.setDefaultMaxPerRoute(2); pool.setMaxTotal(2); // Run the I/O reactor in a separate thread Thread t = new Thread(new Runnable() { public void run() { try { // Ready to go! ioReactor.execute(ioEventDispatch); } catch (InterruptedIOException ex) { System.err.println("Interrupted"); } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); } System.out.println("Shutdown"); } }); // Start the client thread t.start(); // Create HTTP requester // HTTP HttpAsyncRequester requester = new HttpAsyncRequester(httpproc); // Execute HTTP GETs to the following hosts and HttpHost[] targets = new HttpHost[] { new HttpHost("www.baidu.org", -1, "https"), // new HttpHost("www.zhihu.com", -1, "https"), new HttpHost("www.bilibili.com", -1, "https") }; final CountDownLatch latch = new CountDownLatch(targets.length); for (final HttpHost target : targets) { BasicHttpRequest request = new BasicHttpRequest("GET", "/"); HttpCoreContext coreContext = HttpCoreContext.create(); requester.execute(new BasicAsyncRequestProducer(target, request), new BasicAsyncResponseConsumer(), pool, coreContext, // Handle HTTP response from a callback new FutureCallback<HttpResponse>() { public void completed(final HttpResponse response) { latch.countDown(); System.out.println(target + "->" + response.getStatusLine()); } public void failed(final Exception ex) { latch.countDown(); System.err.println(target + "->" + ex); ex.printStackTrace(); } public void cancelled() { latch.countDown(); System.out.println(target + " cancelled"); } }); } latch.await(); System.out.println("Shutting down I/O reactor"); ioReactor.shutdown(); System.out.println("Done"); }
From source file:io.aos.protocol.http.httpcommon.ElementalPoolingHttpGet.java
public static void main(String... args) throws Exception { final HttpParams params = new SyncBasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "Test/1.1"); HttpProtocolParams.setUseExpectContinue(params, true); final HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] { // Required protocol interceptors new RequestContent(), new RequestTargetHost(), // Recommended protocol interceptors new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue() }); final HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); final BasicConnPool pool = new BasicConnPool(new BasicConnFactory(params)); pool.setDefaultMaxPerRoute(2);//w ww . j ava 2 s . c o m pool.setMaxTotal(2); HttpHost[] targets = { new HttpHost("www.google.com", 80), new HttpHost("www.yahoo.com", 80), new HttpHost("www.apache.com", 80) }; class WorkerThread extends Thread { private final HttpHost target; WorkerThread(final HttpHost target) { super(); this.target = target; } @Override public void run() { try { Future<BasicPoolEntry> future = pool.lease(this.target, null); boolean reusable = false; BasicPoolEntry entry = future.get(); try { HttpClientConnection conn = entry.getConnection(); HttpContext context = new BasicHttpContext(null); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target); BasicHttpRequest request = new BasicHttpRequest("GET", "/"); System.out.println(">> Request URI: " + request.getRequestLine().getUri()); request.setParams(params); httpexecutor.preProcess(request, httpproc, context); HttpResponse response = httpexecutor.execute(request, conn, context); response.setParams(params); httpexecutor.postProcess(response, httpproc, context); System.out.println("<< Response: " + response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity())); ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy(); reusable = connStrategy.keepAlive(response, context); } catch (IOException ex) { throw ex; } catch (HttpException ex) { throw ex; } finally { if (reusable) { System.out.println("Connection kept alive..."); } pool.release(entry, reusable); } } catch (Exception ex) { System.out.println("Request to " + this.target + " failed: " + ex.getMessage()); } } } ; WorkerThread[] workers = new WorkerThread[targets.length]; for (int i = 0; i < workers.length; i++) { workers[i] = new WorkerThread(targets[i]); } for (int i = 0; i < workers.length; i++) { workers[i].start(); } for (int i = 0; i < workers.length; i++) { workers[i].join(); } }