java-http-client

Mocking execte() method of HttpClient [duplicate]

爷,独闯天下 提交于 2020-01-06 05:51:08
问题 This question already has an answer here : Mocked HttpClient calls actual method (1 answer) Closed 5 months ago . I want to mock execute method of HttpClass to return a dummy response, but my test code is acutally executing the request. Class Code - class MyService { private CloseableHttpClient newHttpClient() { try { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( new SSLContextBuilder().loadTrustMaterial(TrustAllStrategy.INSTANCE).build(), NoopHostnameVerifier.INSTANCE);

Wrapping BodySubscriber<InputStream> in GZIPInputStream leads to hang

拜拜、爱过 提交于 2019-12-30 10:17:13
问题 I'm using the new java.net.http classes to handle asynchronous HTTP request+response exchanges, and I'm trying to find a way to have the BodySubscriber handle different encoding types such as gzip. However, mapping a BodySubsriber<InputStream> so that the underlying stream is wrapped by a GZIPInputStream (when "Content-Encoding: gzip" is found in the response header) leads to a hang. No exceptions, just a total cessation of activity. The code which maps the BodySubscriber looks like this:

Wrapping BodySubscriber<InputStream> in GZIPInputStream leads to hang

人走茶凉 提交于 2019-12-30 10:16:08
问题 I'm using the new java.net.http classes to handle asynchronous HTTP request+response exchanges, and I'm trying to find a way to have the BodySubscriber handle different encoding types such as gzip. However, mapping a BodySubsriber<InputStream> so that the underlying stream is wrapped by a GZIPInputStream (when "Content-Encoding: gzip" is found in the response header) leads to a hang. No exceptions, just a total cessation of activity. The code which maps the BodySubscriber looks like this:

In Java 11 HttpClient how to solve restricted header name: Date

北慕城南 提交于 2019-12-19 06:39:09
问题 The following java 11 code: HttpRequest request = HttpRequest.newBuilder() .uri(uri) .header("Digest", digest) .header("Date", date) .build(); gives the following error: Exception in thread "main" java.lang.IllegalArgumentException: restricted header name: "Date" The problem is that the digest is based on the date, so I cannot simply rely on the http client date, because that will make the digest invalid. I need a way to either set the Date header, or to retrieve the Date header and then set

In Java 11 HttpClient how to solve restricted header name: Date

安稳与你 提交于 2019-12-19 06:38:03
问题 The following java 11 code: HttpRequest request = HttpRequest.newBuilder() .uri(uri) .header("Digest", digest) .header("Date", date) .build(); gives the following error: Exception in thread "main" java.lang.IllegalArgumentException: restricted header name: "Date" The problem is that the digest is based on the date, so I cannot simply rely on the http client date, because that will make the digest invalid. I need a way to either set the Date header, or to retrieve the Date header and then set

How to log request/response using java.net.http.HttpClient?

℡╲_俬逩灬. 提交于 2019-12-18 15:35:42
问题 The HttpClient introduced experimentally in Java 9 is now stable in Java 11, but not surprisingly, very few projects seem to actually use it. Documentation is almost non-existing. One of the most commons asks while making a HTTP call is logging of request/response. How would you do that using the HttpClient , without of course, logging it manually in every single call? Is there an interceptor mechanism like that offered by all other HTTP clients? 回答1: If we look at jdk.internal.net.http

NoClassDefFoundError while trying to use jdk.incubator.http.HttpClient in java in Eclipse Oxygen [duplicate]

丶灬走出姿态 提交于 2019-12-12 10:53:20
问题 This question already has answers here : Java 9 no class definition exception (3 answers) Closed last year . Here is the code snippet that I use: HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder(URI.create("https://www.google.com")).GET().build(); HttpResponse.BodyHandler responseBodyHandler = HttpResponse.BodyHandler.asString(); HttpResponse response = client.send(request, responseBodyHandler); System.out.println("Status code = " + response

Using Java 11 HttpClient to read chunked data

拈花ヽ惹草 提交于 2019-12-11 13:04:40
问题 I'm trying to read chunked data from an Http Response using Java 11's java.net.http.HttpClient, but I'm only getting one line at a time. I need to get an entire chunk at a time. Here's my code: final InputStream eventStream; try { HttpResponse<InputStream> httpResponse = httpClient.send(HttpRequest .newBuilder( new URI(this.config.getEnvironmentAccess().getUrl() + ":<port>/status/?pretty=true")) .GET().build(), BodyHandlers.ofInputStream()); LOGGER.info("event stream HttpResponse received");

Does Java HTTP Client handle compression

狂风中的少年 提交于 2019-12-10 16:44:35
问题 I tried to find any mention of handling of compression in new Java HTTP Client but failed. Is there a built-in configuration to handle for e.g. gzip or deflate compression? I would expect to have a BodyHandler for e.g. something like this: HttpResponse.BodyHandlers.ofGzipped(HttpResponse.BodyHandlers.ofString()) but I don't see any. I don't see any configuration in HttpClient either. Am I looking in the wrong place or was this intentionally not implemented and deferred to support libraries?

How to map a JSON response to a Java class using Java 11 HttpClient and Jackson?

我的梦境 提交于 2019-12-10 14:46:31
问题 I'm new to the Java 11 HttpClient and would like to give it a try. I have a simple GET request that return JSON and I would like to map the JSON response to a Java class called Questionnaire . I understand that I can turn the response out of box into a String or an input stream like this HttpRequest request = HttpRequest.newBuilder(new URI(String.format("%s%s", this.baseURI, "/state"))) .header(ACCEPT, APPLICATION_JSON) .PUT(noBody()).build(); HttpResponse<String> response = this.client.send