分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
用两种方式分别实现了,第一种是jdk原生的,代码稍微多点,第二种是基于httpclient4版本的。在我的机器上,访问同一个接口原生的性能要好很多(前者900ms,后者5.7s左右),httpclient主要性能消耗在"HttpResponse res = client.execute(post);",大约占总执行时间的90%。
- private static final String METHOD_POST = "POST";
- private static final String DEFAULT_CHARSET = "utf-8";
- public static String doPost(String url, String params, String charset, int connectTimeout, int readTimeout) throws Exception {
- String ctype = "application/json;charset=" + charset;
- byte[] content = {};
- if(params != null){
- content = params.getBytes(charset);
- }
- return doPost(url, ctype, content, connectTimeout, readTimeout);
- }
- public static String doPost(String url, String ctype, byte[] content,int connectTimeout,int readTimeout) throws Exception {
- HttpsURLConnection conn = null;
- OutputStream out = null;
- String rsp = null;
- try {
- try{
- SSLContext ctx = SSLContext.getInstance("TLS");
- ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager()}, new SecureRandom());
- SSLContext.setDefault(ctx);
- conn = getConnection(new URL(url), METHOD_POST, ctype);
- conn.setHostnameVerifier(new Host