|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
java比较简单,没有C++的烦琐,但学习时最好有C++为基础.与JSP和SQL起应用,功能强大.Apache的httpclient项目一向在晋级,从httpclient3.1项目到httpcomponents-client4.0再到4.3都不是太不乱,如今处于4.3版本,终究基础不乱上去。如今httpclient3.1已中断,官方激烈保举晋级到httpcomponents-client。
实在开辟一个功效完全壮大的httpclient客户端仍是不简单的。这里只是贴出一个利用httpcomponents-client里CloseableHttpClient举行http哀求的例子,可是就这几行代码已内置对Cookie和SSL的处置。谈不上很平安,但最少对利用Cookie对Https的URL是能够处置取回内容的。
importjava.security.KeyManagementException;
importjava.security.NoSuchAlgorithmException;
importjava.security.cert.X509Certificate;
importjavax.net.ssl.SSLContext;
importjavax.net.ssl.TrustManager;
importjavax.net.ssl.X509TrustManager;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.conn.ssl.SSLConnectionSocketFactory;
importorg.apache.http.impl.client.CloseableHttpClient;
importorg.apache.http.impl.client.HttpClients;
importorg.apache.http.util.EntityUtils;
publicclassHttpComponentTest{
publicCloseableHttpClientcreateHttpsClient(){
X509TrustManagerx509mgr=newX509TrustManager(){
@Override
publicvoidcheckClientTrusted(X509Certificate[]xcs,Stringstring){
}
@Override
publicvoidcheckServerTrusted(X509Certificate[]xcs,Stringstring){
}
@Override
publicX509Certificate[]getAcceptedIssuers(){
returnnull;
}
};
SSLContextsslContext=null;
try{
sslContext=SSLContext.getInstance("TLS");
}catch(NoSuchAlgorithmExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
try{
sslContext.init(null,newTrustManager[]{x509mgr},null);
}catch(KeyManagementExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
SSLConnectionSocketFactorysslsf=newSSLConnectionSocketFactory(
sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
returnHttpClients.custom().setSSLSocketFactory(sslsf).build();
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args)throwsException{
//CloseableHttpClienthttpclient=HttpClients.createDefault();
HttpComponentTestt=newHttpComponentTest();
CloseableHttpClienthttpclient=t.createHttpsClient();
//HttpGethttpGet=newHttpGet("http://www.google.com.hk/search?hl=zh&ie=utf-8&num=30&output=rss&q=java+%7C+hibernate+%7C+spring&tbm=blg");
HttpGethttpGet=newHttpGet("https://www.google.com.hk/search?hl=zh&ie=utf-8&num=30&output=rss&q=java+%7C+hibernate+%7C+spring&tbm=blg");
//HttpGethttpGet=newHttpGet("http://itindex.net/");
try{
HttpResponsehttpResp=httpclient.execute(httpGet);
intstatusCode=httpResp.getStatusLine().getStatusCode();
if(statusCode==200){
System.out.println(EntityUtils.toString(httpResp.getEntity()));
}
}catch(Exceptione){
e.printStackTrace();
}finally{
httpGet.releaseConnection();
}
}
}
再说说缺点:首先java功能强大的背后是其复杂性,就拿web来说,当今流行的框架有很多,什么struts,spring,jQuery等等,而这无疑增加了java的复杂性。 |
|