回答:
imho:Apache HTTPクライアント
使用例:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.io.*;
public class HttpClientTutorial {
private static String url = "http://www.apache.org/";
public static void main(String[] args) {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
}
いくつかのハイライト機能:
Commons HttpClientの後継であるApache HttpComponents HttpClientをお勧めします
HtmlUnitもご覧になることをお勧めします。HtmlUnitは、「Javaプログラム用のGUIが少ないブラウザー」です。 http://htmlunit.sourceforge.net/
私はジャージーにいくらか不満です。私たちはすべてのプロジェクトで1.10を使用しており、それで解決できない問題に遭遇していません。
気に入った理由:
実際、HTTPClientとJerseyは、実装とAPIが非常に似ています。HTTPClientをサポートできるようにするJerseyの拡張機能もあります。
Jersey 1.xのいくつかのコードサンプル:https : //blogs.oracle.com/enterprisetechtips/entry/sumption_restful_web_services_with
http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/
Jerseyクライアントを備えたHTTPClient:https : //blogs.oracle.com/PavelBucek/entry/jersey_client_apache_http_client
私はhttpclientが標準の何かであることに同意します-しかし、あなたはオプションを探していると思います...
Restletは、Restful Webサービスと対話するために特別に設計されたhttpクライアントを提供します。
コード例:
Client client = new Client(Protocol.HTTP);
Request r = new Request();
r.setResourceRef("http://127.0.0.1:8182/sample");
r.setMethod(Method.GET);
r.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.TEXT_XML));
client.handle(r).getEntity().write(System.out);
詳細については、http://www.restlet.org/を参照してください
corn-httpclientをお勧めします。シンプルで、高速で、ほとんどの場合に十分です。
HttpForm form = new HttpForm(new URI("http://localhost:8080/test/formtest.jsp"));
//Authentication form.setCredentials("user1", "password");
form.putFieldValue("input1", "your value");
HttpResponse response = form.doPost();
assertFalse(response.hasError());
assertNotNull(response.getData());
assertTrue(response.getData().contains("received " + val));
Maven依存
<dependency>
<groupId>net.sf.corn</groupId>
<artifactId>corn-httpclient</artifactId>
<version>1.0.0</version>
</dependency>
Google HTTP Javaクライアントは、AndroidとApp Engineでも実行できるので、見栄えが良いです。
私が言及したい寧非同期のHTTPクライアントライブラリを。私はこれを使用したことがありませんが、私が過去にいつも使用していたApache Httpクライアントと比較して、同僚はそれについて絶賛しています。私はそれがNettyに基づいていることを特に興味を持っていました。これは、私がより身近で高い評価を得ている高性能非同期I / Oフレームワークです。