HttpClient和HttpUrlConnection
2016-07-26 20:05
323 查看
HttpUrlConnection请求
// 创建url对象
URL url = new URL(urlStr);
// 打开链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置链接超时时间为5秒
conn.setConnectTimeout(5000);
// 设置读取超时时间为5秒
conn.setReadTimeout(5000);
// 连接
conn.connect();
// 获取响应码200表示成功
int responseCode = conn.getResponseCode();
// 成功时的处理
if (200 == responseCode) {
// 获取输入流
InputStream is = conn.getInputStream();
//通过ByteArrayOutputStream类,把内容写到内存中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = -1;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
//写到内存
bos.write(buf, 0, len);
}
//将byte数组转成utf-8字符串
final String text = bos.toString("utf-8");
//更新UI,使用runOnUiThread方法
runOnUiThread(new Runnable() {
public void run() {
//显示结果
mTv.setText(text);
}
});
}
HttpClick分为Post和get请求
可以用ByteArrayInputStream将字符串转换为输入流
HttpGet请求
// 首先创建httpGet对象,传入url地址
HttpGet get = new HttpGet(url);
// 创建一个client对象
HttpClient client = new DefaultHttpClient();
// 访问请求,并获取响应对象
HttpResponse resp = client.execute(get);
// 获取状态码
int statusCode = resp.getStatusLine().getStatusCode();
// 判断是否为200-成功
if (HttpStatus.SC_OK == statusCode) {
String content = EntityUtils.toString(resp.getEntity());
return content;
}
return "";
HttpPost请求
public static String readURLByPost(String url, String... params) throws Exception {
// 创建参数list
List<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();
// 把所有参数添加到list中
for (int i = 0; i < params.length; i += 2) {
paramsList.add(new BasicNameValuePair(params[i], params[i + 1]));
}
HttpPost post = new HttpPost(url);
//设置实体参数
post.setEntity(new UrlEncodedFormEntity(paramsList,"utf-8"));
// 下面的跟get请求一样
// 创建一个client对象
HttpClient client = new DefaultHttpClient();
// 访问请求,并获取响应对象
HttpResponse resp = client.execute(post);
// 获取状态码
int statusCode = resp.getStatusLine().getStatusCode();
// 判断是否为200-成功
if (HttpStatus.SC_OK == statusCode) {
String content = EntityUtils.toString(resp.getEntity());
return content;
}
return "";
}
// 创建url对象
URL url = new URL(urlStr);
// 打开链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置链接超时时间为5秒
conn.setConnectTimeout(5000);
// 设置读取超时时间为5秒
conn.setReadTimeout(5000);
// 连接
conn.connect();
// 获取响应码200表示成功
int responseCode = conn.getResponseCode();
// 成功时的处理
if (200 == responseCode) {
// 获取输入流
InputStream is = conn.getInputStream();
//通过ByteArrayOutputStream类,把内容写到内存中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = -1;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
//写到内存
bos.write(buf, 0, len);
}
//将byte数组转成utf-8字符串
final String text = bos.toString("utf-8");
//更新UI,使用runOnUiThread方法
runOnUiThread(new Runnable() {
public void run() {
//显示结果
mTv.setText(text);
}
});
}
HttpClick分为Post和get请求
可以用ByteArrayInputStream将字符串转换为输入流
HttpGet请求
// 首先创建httpGet对象,传入url地址
HttpGet get = new HttpGet(url);
// 创建一个client对象
HttpClient client = new DefaultHttpClient();
// 访问请求,并获取响应对象
HttpResponse resp = client.execute(get);
// 获取状态码
int statusCode = resp.getStatusLine().getStatusCode();
// 判断是否为200-成功
if (HttpStatus.SC_OK == statusCode) {
String content = EntityUtils.toString(resp.getEntity());
return content;
}
return "";
HttpPost请求
public static String readURLByPost(String url, String... params) throws Exception {
// 创建参数list
List<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();
// 把所有参数添加到list中
for (int i = 0; i < params.length; i += 2) {
paramsList.add(new BasicNameValuePair(params[i], params[i + 1]));
}
HttpPost post = new HttpPost(url);
//设置实体参数
post.setEntity(new UrlEncodedFormEntity(paramsList,"utf-8"));
// 下面的跟get请求一样
// 创建一个client对象
HttpClient client = new DefaultHttpClient();
// 访问请求,并获取响应对象
HttpResponse resp = client.execute(post);
// 获取状态码
int statusCode = resp.getStatusLine().getStatusCode();
// 判断是否为200-成功
if (HttpStatus.SC_OK == statusCode) {
String content = EntityUtils.toString(resp.getEntity());
return content;
}
return "";
}
相关文章推荐
- 网络最大流-ISAP算法详解与模板
- TCP/IP各层主要功能
- 网络流-Dinic算法详解与模板
- TCP之长连接与短连接
- tcp/ip概述和wireshark的简单使用
- TCP的分层结构
- 8-20 HttpURLConnection Volley android-async-http WebService WebView笔记待整理
- angularJs的那些坑——$http服务
- HTTP状态码206
- UDP和TCP协议详解
- 网络基础常识
- HTTP请求中的Body构建——.NET客户端调用JAVA服务进行文件上传
- Android基础总结(9)——网络技术
- 程序包org.apache.http不存在,解决方式
- HttpServletRequest request 获取form参数的两种方式
- Ubuntu 14.04 安装 Sublime Text 3
- 高负载系统,内核网络参数的设置 sysctl
- iOS 9网络联网问题
- AFNetworiking与ASIHttpRequest对比
- linux网络编程之简单的服务器多线程