HttpClient和HttpUrlConnection这种方式都支持Https协议,都是以流的形式进行上传或者下载数据,也可以以流的形式进行数据的传输,还有ipv6以及连接池等功能。HttpClient这个拥有非常多的API,所以如果想要进行扩展的话,并且不破坏它的兼容性的话,很难进行扩展,也就是这个原因,谷歌在android6.0的时候,直接就弃用了这个HttpClient.而HttpUrlConnection相对来说就是比较轻量级了,API比较邵,容易扩展,并且能够满足Android大部分的数据传输。比较经典的一个框架Volley,在2.3版本以前都是用HttpClient,在2.3以后就使用了HttpUrlConnetion.
//HttpUrlConnetion简单封装
public class NetUtils{
public static String post(String url, String content) {
HttpURLConnection conn = null;
try {
// 创建一个URL对象
URL mURL = new URL(url);
// 调用URL的openConnection()方法,获取HttpURLConnection对象
conn = (HttpURLConnection) mURL.openConnection();
conn.setRequestMethod("POST");// 设置请求方法为post
conn.setReadTimeout(5000);// 设置读取超时为5秒
conn.setConnectTimeout(10000);// 设置连接网络超时为10秒
conn.setDoOutput(true);// 设置此方法,允许向服务器输出内容
// post请求的参数
String data = content;
// 获得一个输出流,向服务器写数据,默认情况下,系统不允许向服务器输出内容
OutputStream out = conn.getOutputStream();// 获得一个输出流,向服务器写数据
out.write(data.getBytes());
out.flush();
out.close();
int responseCode = conn.getResponseCode();// 调用此方法就不必再使用conn.connect()方法
if (responseCode == 200) {
InputStream is = conn.getInputStream();
String response = getStringFromInputStream(is);
return response;
} else {
throw new NetworkErrorException("response status is "+responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();// 关闭连接
}
}
return null;
}
public static String get(String url) {
HttpURLConnection conn = null;
try {
// 利用string url构建URL对象
URL mURL = new URL(url);
conn = (HttpURLConnection) mURL.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(10000);
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream is = conn.getInputStream();
String response = getStringFromInputStream(is);
return response;
} else {
throw new NetworkErrorException("response status is "+responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
private static String getStringFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
// 模板代码 必须熟练
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
is.close();
String state = os.toString();// 把流中的数据转换成字符串,采用的编码是utf-8(模拟器默认编码)
os.close();
return state;
}
}
//注意需要添加网络权限:
异步通常伴随者他的好兄弟回调。这是通过回调封装的Utils类。 public class AsynNetUtils {
public interface Callback{
void onResponse(String response);
}
public static void get(final String url, final Callback callback){
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
final String response = NetUtils.get(url);
handler.post(new Runnable() {
@Override
public void run() {
callback.onResponse(response);
}
});
}
}).start();
}
public static void post(final String url, final String content, final Callback callback){
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
final String response = NetUtils.post(url,content);
handler.post(new Runnable() {
@Override
public void run() {
callback.onResponse(response);
}
});
}
}).start();
}
}
使用请求:
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.webview);
AsynNetUtils.get("http://www.baidu.com", new AsynNetUtils.Callback() {
@Override
public void onResponse(String response) {
textView.setText(response);
}
});
}
附加简述http和https区别
1.https协议需要到ca申请证书,一般免费的证书比较少,因而需要一定费用。
2.http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输协议。
3.http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443.
4.http的连接很简单,是无状态的;HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。
https实现原理:
1)客户使用https的URL访问Web服务器,要求与Web服务器建立SSL连接。
2)Web服务器收到客户端请求后,会将网站的证书信息(证书中包含公钥)传送一份给客户端。
3)客户端的浏览器与Web服务器开始协商SSL连接的安全等级,也就是信息加密的等级。
4)客户端的浏览器根据双方同意的安全等级,建立会话密钥,然后利用网站的公钥将会话密钥加密,并传送给网站。
5)Web服务器利用自己的私钥解密出会话密钥。
6)Web服务器利用会话密钥加密与客户端之间的通信。