HttpURLConnection是java的标准类,什么都没封装。

HTTPClient是个开源框架,封装了访问http的请求头,参数,内容体,响应等等。

简单来说,HTTPClient就是一个增强版的HttpURLConnection,HttpURLConnection可以做的事情HTTPClient全部可以做;HttpURLConnection没有提供的有些功能,HTTPClient也提供了,但它只是关注于如何发送请求、接收响应,以及管理HTTP连接。

实例:

package com.cn.common.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/http")
public class HttpClientAndURLConnectionController { @RequestMapping("/testHttp")
public void testHttp(HttpServletRequest req,HttpServletResponse resp) {
try {
long s1 = System.currentTimeMillis();
testHttpClient();
long s2 = System.currentTimeMillis();
testHttpURlConnection();
long s3 = System.currentTimeMillis();
System.out.println((s2-s1)+" "+(s3-s2));
} catch (IOException e) {
e.printStackTrace();
}
}
public void testHttpClient() throws IOException{
System.out.println("testHttpClient");
//请求方法
String result="";
String url = "http://192.168.2.111:8088/era/user/getUserById";
HttpClient client =new HttpClient();
//设置连接时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
//解决乱码问题
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
PostMethod method =new PostMethod(url);
//传参
method.addParameter("userId", "100");
int status = client.executeMethod(method);
if(status == HttpStatus.SC_OK){
result = method.getResponseBodyAsString();
}
System.out.println(result);
method.releaseConnection();
}
public void testHttpURlConnection() throws IOException{
System.out.println("testHttpURlConnection");
String result = "";
URL url = new URL("http://192.168.2.111:8088/era/user/getUserById");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30000);//设置连接主机超时(单位:毫秒)
conn.setReadTimeout(30000);//设置从主机读取数据超时(单位:毫秒)
conn.setRequestMethod("POST");//设定请求的方法为"POST",默认是GET
conn.setDoInput(true);//设置是否从httpUrlConnection读入,默认情况下是true
conn.setDoOutput(true);//设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false
conn.setUseCaches(false);//Post请求不能使用缓存
conn.setInstanceFollowRedirects(false);
conn.setRequestProperty("Content-Type", "text/html; charset=utf-8");
conn.connect();
int responsecode = conn.getResponseCode();
if(responsecode == HttpURLConnection.HTTP_OK){ //对应HTTP响应中状态行的响应码
//操作请求流,这里对应HTTP响应中的响应正文
InputStream urlStream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream,"utf-8"));
String s = "";
while ((s = reader.readLine()) != null) {
result += s;
}
reader.close();
urlStream.close();
}
System.out.println(result);
if(conn != null){
conn.disconnect();
}
}
}

调用的url方法:

@RequestMapping("/getUserById")
public void getUserById(HttpServletRequest req,HttpServletResponse resp) throws IOException{
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("UTF-8");
int userId;
if(null == req.getParameter("userId")){
userId = 100;
}else{
userId = Integer.parseInt(req.getParameter("userId"));
}
User user = this.userService.selectByPrimaryKey(userId);
ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(user);
resp.getWriter().print(result);
}

项目启动后,第一次执行testHttp方法后控制台信息:

testHttpClient
2016-07-31 00:24:46,513 DEBUG [org.apache.commons.httpclient.HttpClient] - Java version: 1.7.0_75
2016-07-31 00:24:46,515 DEBUG [org.apache.commons.httpclient.HttpClient] - Java vendor: Oracle Corporation
2016-07-31 00:24:46,515 DEBUG [org.apache.commons.httpclient.HttpClient] - Java class path: D:\apache-tomcat-8.0.33/bin/bootstrap.jar;D:\apache-tomcat-8.0.33/bin/tomcat-juli.jar;D:\Program Files\Java\jdk1.7.0_75/lib/tools.jar
2016-07-31 00:24:46,515 DEBUG [org.apache.commons.httpclient.HttpClient] - Operating system name: Windows 8.1
2016-07-31 00:24:46,515 DEBUG [org.apache.commons.httpclient.HttpClient] - Operating system architecture: amd64
2016-07-31 00:24:46,515 DEBUG [org.apache.commons.httpclient.HttpClient] - Operating system version: 6.3
2016-07-31 00:24:46,516 DEBUG [org.apache.commons.httpclient.HttpClient] - SUN 1.7: SUN (DSA key/parameter generation; DSA signing; SHA-1, MD5 digests; SecureRandom; X.509 certificates; JKS keystore; PKIX CertPathValidator; PKIX CertPathBuilder; LDAP, Collection CertStores, JavaPolicy Policy; JavaLoginConfig Configuration)
2016-07-31 00:24:46,517 DEBUG [org.apache.commons.httpclient.HttpClient] - SunRsaSign 1.7: Sun RSA signature provider
2016-07-31 00:24:46,517 DEBUG [org.apache.commons.httpclient.HttpClient] - SunEC 1.7: Sun Elliptic Curve provider (EC, ECDSA, ECDH)
2016-07-31 00:24:46,517 DEBUG [org.apache.commons.httpclient.HttpClient] - SunJSSE 1.7: Sun JSSE provider(PKCS12, SunX509 key/trust factories, SSLv3, TLSv1)
2016-07-31 00:24:46,517 DEBUG [org.apache.commons.httpclient.HttpClient] - SunJCE 1.7: SunJCE Provider (implements RSA, DES, Triple DES, AES, Blowfish, ARCFOUR, RC2, PBE, Diffie-Hellman, HMAC)
2016-07-31 00:24:46,517 DEBUG [org.apache.commons.httpclient.HttpClient] - SunJGSS 1.7: Sun (Kerberos v5, SPNEGO)
2016-07-31 00:24:46,517 DEBUG [org.apache.commons.httpclient.HttpClient] - SunSASL 1.7: Sun SASL provider(implements client mechanisms for: DIGEST-MD5, GSSAPI, EXTERNAL, PLAIN, CRAM-MD5, NTLM; server mechanisms for: DIGEST-MD5, GSSAPI, CRAM-MD5, NTLM)
2016-07-31 00:24:46,518 DEBUG [org.apache.commons.httpclient.HttpClient] - XMLDSig 1.0: XMLDSig (DOM XMLSignatureFactory; DOM KeyInfoFactory)
2016-07-31 00:24:46,518 DEBUG [org.apache.commons.httpclient.HttpClient] - SunPCSC 1.7: Sun PC/SC provider
2016-07-31 00:24:46,518 DEBUG [org.apache.commons.httpclient.HttpClient] - SunMSCAPI 1.7: Sun's Microsoft Crypto API provider
2016-07-31 00:24:46,535 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.useragent = Jakarta Commons-HttpClient/3.1
2016-07-31 00:24:46,549 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.protocol.version = HTTP/1.1
2016-07-31 00:24:46,556 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.connection-manager.class = class org.apache.commons.httpclient.SimpleHttpConnectionManager
2016-07-31 00:24:46,556 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.protocol.cookie-policy = default
2016-07-31 00:24:46,557 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.protocol.element-charset = US-ASCII
2016-07-31 00:24:46,557 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.protocol.content-charset = ISO-8859-1
2016-07-31 00:24:46,562 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.method.retry-handler = org.apache.commons.httpclient.DefaultHttpMethodRetryHandler@a9d5438
2016-07-31 00:24:46,563 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.dateparser.patterns = [EEE, dd MMM yyyy HH:mm:ss zzz, EEEE, dd-MMM-yy HH:mm:ss zzz, EEE MMM d HH:mm:ss yyyy, EEE, dd-MMM-yyyy HH:mm:ss z, EEE, dd-MMM-yyyy HH-mm-ss z, EEE, dd MMM yy HH:mm:ss z, EEE dd-MMM-yyyy HH:mm:ss z, EEE dd MMM yyyy HH:mm:ss z, EEE dd-MMM-yyyy HH-mm-ss z, EEE dd-MMM-yy HH:mm:ss z, EEE dd MMM yy HH:mm:ss z, EEE,dd-MMM-yy HH:mm:ss z, EEE,dd-MMM-yyyy HH:mm:ss z, EEE, dd-MM-yyyy HH:mm:ss z]
2016-07-31 00:24:46,588 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.connection.timeout = 30000
2016-07-31 00:24:46,588 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.protocol.content-charset = UTF-8
2016-07-31 00:24:46,727 DEBUG [org.apache.commons.httpclient.HttpConnection] - Open connection to 192.168.2.111:8088
2016-07-31 00:24:46,744 DEBUG [httpclient.wire.header] - >> "POST /era/user/getUserById HTTP/1.1[\r][\n]"
2016-07-31 00:24:46,747 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Adding Host request header
2016-07-31 00:24:46,871 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Default charset used: UTF-8
2016-07-31 00:24:46,900 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Default charset used: UTF-8
2016-07-31 00:24:46,902 DEBUG [httpclient.wire.header] - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
2016-07-31 00:24:46,903 DEBUG [httpclient.wire.header] - >> "Host: 192.168.2.111:8088[\r][\n]"
2016-07-31 00:24:46,903 DEBUG [httpclient.wire.header] - >> "Content-Length: 10[\r][\n]"
2016-07-31 00:24:46,903 DEBUG [httpclient.wire.header] - >> "Content-Type: application/x-www-form-urlencoded[\r][\n]"
2016-07-31 00:24:46,907 DEBUG [httpclient.wire.header] - >> "[\r][\n]"
2016-07-31 00:24:46,911 DEBUG [httpclient.wire.content] - >> "userId=100"
2016-07-31 00:24:46,913 DEBUG [org.apache.commons.httpclient.methods.EntityEnclosingMethod] - Request body sent
2016-07-31 00:24:47,041 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2016-07-31 00:24:47,074 DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@631e7edd] was not registered for synchronization because synchronization is not active
2016-07-31 00:24:47,168 DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@155b4677] will not be managed by Spring
2016-07-31 00:24:47,197 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - ==> Preparing: select id, user_name, password, age from user where id = ?
2016-07-31 00:24:47,756 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - ==> Parameters: 100(Integer)
2016-07-31 00:24:47,848 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - <== Total: 1
2016-07-31 00:24:47,852 DEBUG [com.alibaba.druid.pool.PreparedStatementPool] - {conn-10001, pstmt-20000} enter cache
2016-07-31 00:24:47,854 DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@631e7edd]
2016-07-31 00:24:48,085 DEBUG [httpclient.wire.header] - << "HTTP/1.1 200 OK[\r][\n]"
2016-07-31 00:24:48,086 DEBUG [httpclient.wire.header] - << "HTTP/1.1 200 OK[\r][\n]"
2016-07-31 00:24:48,089 DEBUG [httpclient.wire.header] - << "Server: Apache-Coyote/1.1[\r][\n]"
2016-07-31 00:24:48,090 DEBUG [httpclient.wire.header] - << "Content-Type: text/html;charset=UTF-8[\r][\n]"
2016-07-31 00:24:48,090 DEBUG [httpclient.wire.header] - << "Content-Length: 67[\r][\n]"
2016-07-31 00:24:48,090 DEBUG [httpclient.wire.header] - << "Date: Sat, 30 Jul 2016 16:24:48 GMT[\r][\n]"
2016-07-31 00:24:48,091 DEBUG [httpclient.wire.header] - << "[\r][\n]"
2016-07-31 00:24:48,095 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Buffering response body
2016-07-31 00:24:48,095 DEBUG [httpclient.wire.content] - << "{"id":100,"userName":"admin[0xe7][0xae][0xa1][0xe7][0x90][0x86][0xe5][0x91][0x98]","password":"123456","age":25}"
2016-07-31 00:24:48,097 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Resorting to protocol version default close connection policy
2016-07-31 00:24:48,097 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Should NOT close connection, using HTTP/1.1
2016-07-31 00:24:48,098 DEBUG [org.apache.commons.httpclient.HttpConnection] - Releasing connection back to connection manager.
{"id":100,"userName":"admin管理员","password":"123456","age":25}
testHttpURlConnection
2016-07-31 00:24:48,127 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2016-07-31 00:24:48,129 DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ee42c08] was not registered for synchronization because synchronization is not active
2016-07-31 00:24:48,129 DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@155b4677] will not be managed by Spring
2016-07-31 00:24:48,130 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - ==> Preparing: select id, user_name, password, age from user where id = ?
2016-07-31 00:24:48,131 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - ==> Parameters: 100(Integer)
2016-07-31 00:24:48,137 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - <== Total: 1
2016-07-31 00:24:48,138 DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ee42c08]
{"id":100,"userName":"admin管理员","password":"123456","age":25}
1598 58

第一次执行后:

testHttpClient()用时:1598

testHttpURlConnection()用时:58

第二次执行testHttp方法后控制台信息:

testHttpClient
2016-07-31 00:28:09,582 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.connection.timeout = 30000
2016-07-31 00:28:09,582 DEBUG [org.apache.commons.httpclient.params.DefaultHttpParams] - Set parameter http.protocol.content-charset = UTF-8
2016-07-31 00:28:09,583 DEBUG [org.apache.commons.httpclient.HttpConnection] - Open connection to 192.168.2.111:8088
2016-07-31 00:28:09,586 DEBUG [httpclient.wire.header] - >> "POST /era/user/getUserById HTTP/1.1[\r][\n]"
2016-07-31 00:28:09,587 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Adding Host request header
2016-07-31 00:28:09,587 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Default charset used: UTF-8
2016-07-31 00:28:09,590 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Default charset used: UTF-8
2016-07-31 00:28:09,591 DEBUG [httpclient.wire.header] - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
2016-07-31 00:28:09,592 DEBUG [httpclient.wire.header] - >> "Host: 192.168.2.111:8088[\r][\n]"
2016-07-31 00:28:09,592 DEBUG [httpclient.wire.header] - >> "Content-Length: 10[\r][\n]"
2016-07-31 00:28:09,593 DEBUG [httpclient.wire.header] - >> "Content-Type: application/x-www-form-urlencoded[\r][\n]"
2016-07-31 00:28:09,593 DEBUG [httpclient.wire.header] - >> "[\r][\n]"
2016-07-31 00:28:09,593 DEBUG [httpclient.wire.content] - >> "userId=100"
2016-07-31 00:28:09,594 DEBUG [org.apache.commons.httpclient.methods.EntityEnclosingMethod] - Request body sent
2016-07-31 00:28:09,601 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2016-07-31 00:28:09,602 DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@240543cf] was not registered for synchronization because synchronization is not active
2016-07-31 00:28:09,603 DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@155b4677] will not be managed by Spring
2016-07-31 00:28:09,605 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - ==> Preparing: select id, user_name, password, age from user where id = ?
2016-07-31 00:28:09,606 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - ==> Parameters: 100(Integer)
2016-07-31 00:28:09,611 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - <== Total: 1
2016-07-31 00:28:09,612 DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@240543cf]
2016-07-31 00:28:09,616 DEBUG [httpclient.wire.header] - << "HTTP/1.1 200 OK[\r][\n]"
2016-07-31 00:28:09,617 DEBUG [httpclient.wire.header] - << "HTTP/1.1 200 OK[\r][\n]"
2016-07-31 00:28:09,617 DEBUG [httpclient.wire.header] - << "Server: Apache-Coyote/1.1[\r][\n]"
2016-07-31 00:28:09,617 DEBUG [httpclient.wire.header] - << "Content-Type: text/html;charset=UTF-8[\r][\n]"
2016-07-31 00:28:09,618 DEBUG [httpclient.wire.header] - << "Content-Length: 67[\r][\n]"
2016-07-31 00:28:09,618 DEBUG [httpclient.wire.header] - << "Date: Sat, 30 Jul 2016 16:28:09 GMT[\r][\n]"
2016-07-31 00:28:09,618 DEBUG [httpclient.wire.header] - << "[\r][\n]"
2016-07-31 00:28:09,618 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Buffering response body
2016-07-31 00:28:09,619 DEBUG [httpclient.wire.content] - << "{"id":100,"userName":"admin[0xe7][0xae][0xa1][0xe7][0x90][0x86][0xe5][0x91][0x98]","password":"123456","age":25}"
2016-07-31 00:28:09,619 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Resorting to protocol version default close connection policy
2016-07-31 00:28:09,619 DEBUG [org.apache.commons.httpclient.HttpMethodBase] - Should NOT close connection, using HTTP/1.1
2016-07-31 00:28:09,620 DEBUG [org.apache.commons.httpclient.HttpConnection] - Releasing connection back to connection manager.
{"id":100,"userName":"admin管理员","password":"123456","age":25}
testHttpURlConnection
2016-07-31 00:28:09,629 DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2016-07-31 00:28:09,630 DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4636317d] was not registered for synchronization because synchronization is not active
2016-07-31 00:28:09,631 DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@155b4677] will not be managed by Spring
2016-07-31 00:28:09,631 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - ==> Preparing: select id, user_name, password, age from user where id = ?
2016-07-31 00:28:09,632 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - ==> Parameters: 100(Integer)
2016-07-31 00:28:09,637 DEBUG [com.cn.eagle.dao.UserMapper.selectByPrimaryKey] - <== Total: 1
2016-07-31 00:28:09,640 DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4636317d]
{"id":100,"userName":"admin管理员","password":"123456","age":25}
39 25

第二次执行后:

testHttpClient()用时:39

testHttpURlConnection()用时:25

总的来说,在大压力或者连续不断发请求的情况下,HttpClient不会比HttpURLConnection慢多少,因为保持了底层的Socket连接,不用每次都重新连接。

HTTPClient和HttpURLConnection实例对比的更多相关文章

  1. HttpClient和HttpURLConnection的使用和区别(下)

    转自来自点击打开链接 接着上一篇,我们继续来分析HttpURLConnection的使用,以及两者的共同点和区别. 目录 用法 HttpURLConnection 区别 引用资料 用法 HttpURL ...

  2. java分别通过httpclient和HttpURLConnection获取图片验证码内容

    前面的文章,介绍了如何通过selenium+Tesseract-OCR来识别图片验证码,如果用接口来访问的话,再用selenium就闲的笨重,下面就介绍一下分别通过httpclient和HttpURL ...

  3. Android 网络编程之---HttpClient 与 HttpURLConnection 共用cookie

    HttpClient 与 HttpURLConnection 共用 SessionId HttpClient 与 HttpUrlConnection 是Android 中HTTP操作最常见的訪问方式. ...

  4. 5种样式实现div容器中三图摆放实例对比说明

    代码地址如下:http://www.demodashi.com/demo/11593.html 效果演示: demo点查看效果 需求说明: 如下图所示为设计图,希望在图片上传无规则无规律的情况下实现设 ...

  5. 为HttpClient和HttpURLConnection添加中国移动代理

    转自: http://www.2cto.com/kf/201111/112100.html 在android中,一般需要联网的时候前,都要做一次网络的判断,判断当前的网络状态!然后开始请求网络 当我们 ...

  6. Java8的Stream方法findAny空指针异常(NullPointerException)实例对比

    实战介绍 学习完Java8的Stream方法,可能你正准备大展身手,却发现遇到不少问题,本篇文章为大家带来一个findAny方法抛出java.lang.NullPointerException的场景. ...

  7. HttpClient和HttpURLConnection整合汇总对比

    性能 1.HttpUrlConnection直接支持GZIP压缩:HttpClient也支持,但要自己写代码处理. 2.HttpUrlConnection直接支持系统级连接池,即打开的连接不会直接关闭 ...

  8. Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比

    比较的指标: 1.cpu 2.流量 3.电量 4.内存占用 5.联网时间 功能点: 1.重试机制 2.提供的扩展功能 3.易用性 4.是否https 5.是否支持reflect api,OkHttp有 ...

  9. 关于HttpClient,HttpURLConnection,OkHttp的用法

    1 HttpClient入门实例 1.1发送get请求 /** * HttpClient发送get请求 * @param url 请求地址 * @return * @throws IOExceptio ...

随机推荐

  1. Lucene学习入门——下载初识

    本文从官网下载Lucene开始,一步一步进行Lucene的应用学习研究.下载初识Snowball Stemmer 1.下载 (1)首先,去Lucne的Apache官网主页 http://lucene. ...

  2. 通过HTTP响应头让浏览器自动刷新

    以前如果需要让网页过几秒自动刷新一次,我都会在页面通过JS调用setTimeout来做,最近发现原来服务器通过添加响应头部信息来提示浏览器需要在多少时间之后重新加载页面. 代码很简单: respons ...

  3. vos语音业务规范操作承诺函

    语音业务规范操作承诺函 *****网络科技有限公司: 我司开通固话2000线,话务价格为0.045元/分钟(仅拨打中国移动网内电话).为防止投诉.网外电话.欠费等语音话务风险,我司承诺如下:在业务上遵 ...

  4. 正交矩阵、正规矩阵和酉矩阵(转自Ramble Over The Cloud~)

    网址:http://blog.csdn.net/alec1987/article/details/7414450 在数学中,正规矩阵 是与自己的共轭转置交换的复系数方块矩阵,也就是说, 满足 其中 是 ...

  5. 【LOJ115】无源汇有上下界可行流(模板题)

    点此看题面 大致题意: 给你每条边的流量上下界,让你判断是否存在可行流.若有,则还需输出一个合法方案. 大致思路 首先,每条边既然有一个流量下界\(lower\),我们就强制它初始流量为\(lower ...

  6. ssh key一键自动化生成公钥私钥,并自动分发上百服务器免密码交互

    题记:由于工作需要管理大量服务器,所以需要配公钥实现免密登录. ssh批量分发可以一键执行这个操作,但是使用ssh分发服务还需要对各个服务器进行.ssh/id_dsa.pub公钥上传,密码验证.所以需 ...

  7. 2017.10.12 Java的计数器的开发

    //我们用一个合成的applet/application来简单显示出一个计数器的结果/** * Created by qichunlin on 2017/10/12. */ /*简单的计数器*/ im ...

  8. c# 语法 字符串内插

    结果截屏 参考文章 https://www.cnblogs.com/csproj/p/Interpolated_Strings.html c# 6.0语法 https://www.cnblogs.co ...

  9. Javascript入门笔记1-script标签

    1.script标签在HTML文件中添加JavaScript代码. JavaScript代码只能写在HTML文件中吗?当然不是,我们可以把HTML文件和JS代码分开,并单独创建一个JavaScript ...

  10. 【思维题 细节】loj#6042. 「雅礼集训 2017 Day7」跳蚤王国的宰相

    挂于±1的细节…… 题目描述 跳蚤王国爆发了一场动乱,国王在镇压动乱的同时,需要在跳蚤国地方钦定一个人来做宰相. 由于当时形势的复杂性,很多跳蚤都并不想去做一个傀儡宰相,带着宰相的帽子,最后还冒着被打 ...