public class Http {
/**
* Http Get fashion
*
* @param url
* @return
*/
public static String doGet(String url) {
String data = null;
// 初始化Http请求对象
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = null;
try {
// 执行请求
try {
httpResponse = httpClient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
}
// 请求到的数据
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
// 得到返回数据:流数据
InputStream inputStream = httpEntity.getContent();
data = convertStreamToString(inputStream);
// 缓存请求结果,目前不实现
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("jsonResult", data);
return data;
}
/**
* Http post fashion
*
* @param url
* :请求地址
* @param postData
* : 请求数据
* @return
*/
public static String doPost(String url, Map<String, String> postData) {
String data = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = null;
// 整理数据
ArrayList<BasicNameValuePair> clearUpData = new ArrayList<BasicNameValuePair>();
if (postData != null) {
Set<String> keys = postData.keySet();
for (Iterator<String> i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
clearUpData.add(new BasicNameValuePair(key, (String) postData
.get(key)));
}
}
try {
// utf-8编码
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(clearUpData,
"utf-8");
// 设置最终数据
httpPost.setEntity(entity);
// 执行
httpResponse = httpClient.execute(httpPost);
// 请求到的数据
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
// 得到返回数据:流数据
InputStream inputStream = httpEntity.getContent();
data = convertStreamToString(inputStream);
// 缓存请求结果,目前不实现
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("jsonResult", data);
return data;
}
/**
* 将流转换为字符串
*
* @param is
* @return
*/
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
分享到:
相关推荐
http请求工具类HttpClientUtil,get,post请求(csdn)————程序
这是来自Bayden系统的浏览器辅助对象。它非常简单,只有两个选项——篡改GET和/或POST。当我们提交这样的请求时,该工具会自动阻断提交,我们可以查看和修改我们提交的信息。这是单单使用IE地址栏所不能完成的内容。
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery...
50个用户 重复100次 发送GET参数 /usr/local/siege/bin/siege -c 50 -r 100 http://www.baidu.com/s?wd=siege&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn=baiduhome_pg&rsv_sug3=4&rsv_sug=2&rsv_sug1=4&rsv_sug4=60 ...
当前版本支持get和post的http协议,项目还在为通用协议的扩展开发中,敬请关注后续更新。 特征: 这个客户端测试工具可以很容易地以少量的 CPU 和内存资源达到很高的压力水平。 源代码规模小,很容易实现其他协议...
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery...
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery...
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery...
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery...
第8章 当Android与Internet接轨 8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse ...
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入...
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery...
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery...
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery...
8.1 HTTPGET/POST传递参数——HTTP连接示范 8.2 在程序里浏览网页——WebView.loadUrl 8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery...
Requests提供了一致的API,并且使用cURL或fsockopen(具体取决于可用性),它还简化了发送请求,接受HEAD、GET、POST、PUT、DELETE或PATCH并且添加标头、表单数据和多个部分组成的文件。其次,该库还适用于国际域名...
学习爬虫之前呢,最好要对HTTP协议有了解,我前面有写过几篇关于HTTP协议的文章,建议读者阅读一下,对爬虫帮助很大。...向HTML页面提交POST请求的方法,对应于HTTP的POST requests.put() 向HTML页面
Requests提供了一致的API,并且使用cURL或fsockopen(具体取决于可用性),它还简化了发送请求,接受HEAD、GET、POST、PUT、DELETE或PATCH并且添加标头、表单数据和多个部分组成的文件。其次,该库还适用于国际域名...