use of org.apache.hc.core5.util.Args in project mercury by yellow013.
the class ClientExecuteProxy method main.
public static void main(final String[] args) throws Exception {
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
final HttpHost target = new HttpHost("https", "httpbin.org", 443);
final HttpHost proxy = new HttpHost("http", "127.0.0.1", 8080);
final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
final HttpGet request = new HttpGet("/get");
request.setConfig(config);
System.out.println("Executing request " + request.getMethod() + " " + request.getUri() + " via " + proxy);
try (final CloseableHttpResponse response = httpclient.execute(target, request)) {
System.out.println("----------------------------------------");
System.out.println(response.getCode() + " " + response.getReasonPhrase());
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
}
use of org.apache.hc.core5.util.Args in project mercury by yellow013.
the class ClientExecuteSOCKS method main.
public static void main(final String[] args) throws Exception {
final Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new MyConnectionSocketFactory()).build();
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg);
try (final CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build()) {
final InetSocketAddress socksaddr = new InetSocketAddress("mysockshost", 1234);
final HttpClientContext context = HttpClientContext.create();
context.setAttribute("socks.address", socksaddr);
final HttpHost target = new HttpHost("http", "httpbin.org", 80);
final HttpGet request = new HttpGet("/get");
System.out.println("Executing request " + request.getMethod() + " " + request.getUri() + " via SOCKS proxy " + socksaddr);
try (final CloseableHttpResponse response = httpclient.execute(target, request, context)) {
System.out.println("----------------------------------------");
System.out.println(response.getCode() + " " + response.getReasonPhrase());
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
}
use of org.apache.hc.core5.util.Args in project mercury by yellow013.
the class ClientFormLogin method main.
public static void main(final String[] args) throws Exception {
final BasicCookieStore cookieStore = new BasicCookieStore();
try (final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build()) {
final HttpGet httpget = new HttpGet("https://someportal/");
try (final CloseableHttpResponse response1 = httpclient.execute(httpget)) {
final HttpEntity entity = response1.getEntity();
System.out.println("Login form get: " + response1.getCode() + " " + response1.getReasonPhrase());
EntityUtils.consume(entity);
System.out.println("Initial set of cookies:");
final List<Cookie> cookies = cookieStore.getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i));
}
}
}
final ClassicHttpRequest login = ClassicRequestBuilder.post().setUri(new URI("https://someportal/")).addParameter("IDToken1", "username").addParameter("IDToken2", "password").build();
try (final CloseableHttpResponse response2 = httpclient.execute(login)) {
final HttpEntity entity = response2.getEntity();
System.out.println("Login form get: " + response2.getCode() + " " + response2.getReasonPhrase());
EntityUtils.consume(entity);
System.out.println("Post logon cookies:");
final List<Cookie> cookies = cookieStore.getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i));
}
}
}
}
}
use of org.apache.hc.core5.util.Args in project mercury by yellow013.
the class ClientInterceptors method main.
public static void main(final String[] args) throws Exception {
try (final CloseableHttpClient httpclient = HttpClients.custom().addRequestInterceptorFirst(new HttpRequestInterceptor() {
private final AtomicLong count = new AtomicLong(0);
@Override
public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
request.setHeader("request-id", Long.toString(count.incrementAndGet()));
}
}).addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new ExecChainHandler() {
@Override
public ClassicHttpResponse execute(final ClassicHttpRequest request, final ExecChain.Scope scope, final ExecChain chain) throws IOException, HttpException {
final Header idHeader = request.getFirstHeader("request-id");
if (idHeader != null && "13".equalsIgnoreCase(idHeader.getValue())) {
final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NOT_FOUND, "Oppsie");
response.setEntity(new StringEntity("bad luck", ContentType.TEXT_PLAIN));
return response;
} else {
return chain.proceed(request, scope);
}
}
}).build()) {
for (int i = 0; i < 20; i++) {
final HttpGet httpget = new HttpGet("http://httpbin.org/get");
System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
try (final CloseableHttpResponse response = httpclient.execute(httpget)) {
System.out.println("----------------------------------------");
System.out.println(response.getCode() + " " + response.getReasonPhrase());
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
}
}
use of org.apache.hc.core5.util.Args in project mercury by yellow013.
the class ClientMultipartFormPost method main.
public static void main(final String[] args) throws Exception {
if (args.length != 1) {
System.out.println("File path not given");
System.exit(1);
}
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
final HttpPost httppost = new HttpPost("http://localhost:8080" + "/servlets-examples/servlet/RequestInfoExample");
final FileBody bin = new FileBody(new File(args[0]));
final StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
final HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build();
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost);
try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
System.out.println("----------------------------------------");
System.out.println(response);
final HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
}
}
}
Aggregations