在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:timboudreau/netty-http-client开源软件地址:https://github.com/timboudreau/netty-http-client开源编程语言:Java 99.5%开源软件介绍:Netty HTTP ClientThis project is fairly obsolete - there is a good HTTP client in the JDK now. Use it. This project will be maintained for a while yet, due to use of the adjacent HTTP test-harness that uses this library under the hood. An asynchronous http client in Java, with a clean, callback-based API, using Netty 4.x. The API is inspired a bit by Node.js
You can use it from Maven projects as described here. Features
Read the javadoc. The header writing/parsing classes come from acteur-util; some URL-related classes are documented here. To use with Maven, add the Maven repo to your project as described here. Then add groupId This project also comes with a test harness which is easy to integrate with unit tests, with built-in methods to assert that the response is what you expect, e.g. harness.get("static/hello.txt").setTimeout(Duration.seconds(1)).go()
.assertHasContent()
.assertStatus(OK)
.assertHasHeader(Headers.LAST_MODIFIED.name())
.assertHasHeader(Headers.ETAG.name())
.assertContent("hello world")
.getHeader(Headers.LAST_MODIFIED); See the bottom of this document for test harness documentation. UsageThe first thing you need is an HttpClient client = HttpClient.builder().followRedirects().build(); The API is callback-based. While you can block the current thread until a response is received using There are two ways to pay attention to the results of an HTTP call - you can listen
for ResponseFuture h = client
.get().setURL ( "http://localhost:9333/foo/bar" ))
.execute ( new ResponseHandler <String> ( String.class ) {
protected void receive ( HttpResponseStatus status, HttpHeaders headers, String response ) {
System.out.println ( "Here's the response: '" + response + "'" );
}
}); (ResponseHandler has additional methods you can override to detect error responses, timeouts or refused connections) You'll note the The DetailsYou can get all the details
by providing a ResponseFuture f = client.get()
.setURL( "http://localhost:9333/foo/bar" )
.setBody( "This is a test", MediaType.PLAIN_TEXT_UTF_8)
.onEvent( new Receiver<State<?>>() {
public void receive( State<?> state ) {
System.out.println( "STATE " + state + " " + state.name() + " " + state.get() );
if ( state.stateType() == StateType.Finished ) {
DefaultFullHttpResponse d = (DefaultFullHttpResponse) state.get();
//do something
}
}
}).execute(); Status & To-DosThis is a young library; it works, but it will surely need some polish yet; and Netty 4.x is still changing, including occasional incompatible changes. Here are some things that would be useful to add:
LicenseMIT license - do what thou wilt, give credit where it's due Test Harness (netty-http-test-harness)Alongside this project is the The point is to make it very little code or setup to test something. Basically you construct a Here's an example: DateTime helloLastModified = har.get("static/hello.txt").go()
.assertHasContent()
.assertStatus(OK)
.assertHasHeader(Headers.LAST_MODIFIED.name())
.assertHasHeader(Headers.ETAG.name())
.assertContent(HELLO_CONTENT)
.getHeader(Headers.LAST_MODIFIED);
DateTime aLastModified = har.get("static/another.txt").go()
.assertStatus(OK)
.assertHasContent()
.assertHasHeader(Headers.LAST_MODIFIED.name())
.assertHasHeader(Headers.ETAG.name())
.assertContent("This is another file. It has some data in it.\n")
.getHeader(Headers.LAST_MODIFIED);
assertNotNull(helloLastModified);
assertNotNull(aLastModified);
har.get("static/hello.txt")
.addHeader(Headers.IF_MODIFIED_SINCE, helloLastModified)
.go()
.assertStatus(NOT_MODIFIED);
har.get("static/another.txt")
.addHeader(Headers.IF_MODIFIED_SINCE, aLastModified)
.go().assertStatus(NOT_MODIFIED); |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论