一般情况下我们需要使用HttpClient时可供选择的技术有:
1、HttpURLConnection
2、Apache HttpClient
其他的除了写Socket 我都没有用过了。
偶然的机会发现Jetty 里面也自带了一个HttpClient,并且支持事件触发的处理方式。
还可以使用同步的处理方式:
还可以方便的设置Proxy网关:
jetty http client 感觉不错,比Apache HttpClient更加易用、小巧!
1、HttpURLConnection
2、Apache HttpClient
其他的除了写Socket 我都没有用过了。
偶然的机会发现Jetty 里面也自带了一个HttpClient,并且支持事件触发的处理方式。
HttpClient client = new HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
try
{
client.start();
}
catch (Exception e)
{
throw new ServletException(e);
}
// create the exchange object, which lets you define where you want to go
// and what you want to do once you get a response
ContentExchange exchange = new ContentExchange()
{
// define the callback method to process the response when you get it back
protected void onResponseComplete() throws IOException
{
super.onResponseComplete();
String responseContent = this.getResponseContent();
// do something with the response content
...
}
};
exchange.setMethod("GET");
exchange.setURL("http://www.example.com/");
// start the exchange
client.send(exchange);
还可以使用同步的处理方式:
public static void main(String[] args)
{
HttpClient httpClient = new HttpClient();
//set up httpClient
httpClient.start();
ContentExchange contentExchange = new ContentExchange();
httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
contentExchange.setURL("http://slashdot.org");
httpClient.send();
contentExchange.waitForDone();
System.err.println("Response status: "+contentExchange.getResponseStatus());
}
还可以方便的设置Proxy网关:
client.setProxy(new Address("proxy_address",proxy_port));
client.setProxyAuthentication(new ProxyAuthorization("user","password"));
jetty http client 感觉不错,比Apache HttpClient更加易用、小巧!