公共代码参考(httpclient)
public class HttpClientUtils {
private static final String CHARSET = "UTF-8";
/*
* http get请求
*
* @param url
*
* @param params
*
* @return
*/
public static String httpGet(String url, Map<String, String> params) {
if (true == StringUtils.isEmpty(url)) {
return null;
}
List<NameValuePair> pl = getParamList(params);
// 转换参数
if (null != pl) {
String urlparams = URLEncodedUtils.format(pl, CHARSET);
url = url + "?" + urlparams;
}
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
try {
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (null != entity) {
return EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*
* http post请求
*
* @param url
*
* @param params
*
* @return
*/
public static String httpPost(String url, Map<String, String> params) {
if (true == StringUtils.isEmpty(url)) {
return null;
}
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> pl = getParamList(params);
// 转换参数
if (null != pl) {
HttpEntity entity;
try {
entity = new UrlEncodedFormEntity(pl);
httppost.setEntity(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (null != entity) {
return EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*
* 创建标准的NameValuePair格式参数
*
* @param params
*
* @return
*/
public static List<NameValuePair> getParamList(Map<String, String> params) {
if (null == params || 0 == params.size()) {
return null;
}
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : params.entrySet()) {
list.add(new BasicNameValuePair(param.getKey(), param.getValue()));
}
return list;
}
}
- 测试用例
public class HttpClientUtilsTest extends TestCase {
private static final String LOG_TAG = "HttpClientUtilsTest";
public void testHttpGet1(){
String url = "http://192.168.1.101:3000/gettest";
String result = HttpClientUtils.httpGet(url, null);
System.out.println(result);
Log.i(LOG_TAG, result);
}
public void testHttpGet2(){
String url = "http://192.168.1.101:3000/gettest";
Map<String,String> map = new HashMap<String,String>();
map.put("username", "fredric");
map.put("password", "fredrictoo");
String result = HttpClientUtils.httpGet(url, map);
System.out.println(result);
Log.i(LOG_TAG, result);
}
public void testHttpPost1(){
String url = "http://192.168.1.101:3000/posttest";
Map<String,String> map = new HashMap<String,String>();
map.put("username", "sinny");
map.put("password", "sinnytoo");
String result = HttpClientUtils.httpPost(url, map);
System.out.println(result);
Log.i(LOG_TAG, result);
}
}
公共代码参考(httpclient)的更多相关文章
- 公共代码参考(Volley)
Volley 是google提供的一个网络库,相对于自己写httpclient确实方便很多,本文参考部分网上例子整理如下,以作备忘: 定义一个缓存类: public class BitmapCache ...
- Session id实现通过Cookie来传输方法及代码参考
1. Web中的Session指的就是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间.因此从上述的定义中我们可以看到,Session实际上是一个特定的 ...
- 如何在vuejs中抽出公共代码
当我们在使用vue构建中大型项目时,通常会遇到某些经常用的方法以及属性,比如说搭建一个员工管理系统,请求的url需要一个共同的前缀,或者在某几个view中需要用到时间,这个时间是通过某方法格式化之后的 ...
- webpack学习笔记--提取公共代码
为什么需要提取公共代码 大型网站通常会由多个页面组成,每个页面都是一个独立的单页应用. 但由于所有页面都采用同样的技术栈,以及使用同一套样式代码,这导致这些页面之间有很多相同的代码. 如果每个页面的代 ...
- [转] 用webpack的CommonsChunkPlugin提取公共代码的3种方式
方式一,传入字符串参数 new webpack.optimize.CommonsChunkPlugin(‘common.js’), // 默认会把所有入口节点的公共代码提取出来,生成一个common. ...
- webpack4 系列教程(三): 多页面解决方案--提取公共代码
这节课讲解webpack4打包多页面应用过程中的提取公共代码部分.相比于webpack3,4.0版本用optimization.splitChunks配置替换了3.0版本的CommonsChunkPl ...
- 详解用webpack的CommonsChunkPlugin提取公共代码的3种方式(注意webpack4.0版本已不存在)
Webpack 的 CommonsChunkPlugin 插件,负责将多次被使用的 JS 模块打包在一起. CommonsChunkPlugin 能解决的问题 在使用插件前,考虑几个问题: 对哪些 c ...
- webpack配置提取公共代码
公共代码提取功能是针对多入口文件的: 背景:在pageA.js和pageB.js中分别引用subPageA.js和subPageB.js webpack.config.js文件: var path = ...
- 系统管理模块_部门管理_改进_抽取添加与修改JSP页面中的公共代码_在显示层抽取BaseAction_合并Service层与Dao层
系统管理模块_部门管理_改进1:抽取添加与修改JSP页面中的公共代码 commons.jspf <%@ page language="java" import="j ...
随机推荐
- 解决root用户ssh配置无密码登陆/hadoop用户照仿可以实现相同功能:hadoop用户登录并且把命令的所有root换成home/hadoop
http://inuyasha1027.blog.51cto.com/4003695/1132896/ 主机ip:192.168.163.100(hostname: node0) ssh无密码登陆的远 ...
- IOS 本地推送 IOS10.0以上 static的作用 const的作用
//需要在AppDelegate里面启动APP的函数 加上 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNot ...
- 蓝牙BLE实用教程
蓝牙BLE实用教程 Bluetooth BLE 欢迎使用 小书匠(xiaoshujiang)编辑器,您可以通过 设置 里的修改模板来改变新建文章的内容. 1.蓝牙BLE常见问答 Q: Smart Re ...
- 封装ios静态库碰到的一些问题(二)
在静态库建立好了之后呢,于是应用程序就引用它,加上拷贝的h文件,但是引用之后Build之后提示很多sybmbol 重复 于是进行检查,确实由于是从其他工程修改过来的,很多基础库都引用了,删除之,最后就 ...
- 封装ios静态库碰到的一些问题(一)
封装IOS动态库,碰到的第一个问题,就是资源文件的问题,如果将你的程序封装成为静态库,那么静态库中不会包含资源文件和xib文件,这个时候就需要自己封装bundle文件了,而笔者开发环境默认是xcode ...
- Android Service完全解析,关于服务你所需知道的一切(下)
转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在上一篇文章中,我们学习了Android Service相关的许多重要 ...
- UI Automation Test
UI Automation test is based on the windows API. U can find the UI Automation MSDN file from http://m ...
- 关于c++
http://www.ezlippi.com/blog/2014/12/c-open-project.html
- ruby 学习笔记 1
写ruby blog 系统的记录下.也是对我学ruby的点滴记录. 先介绍下我的学习环境.系统:ubuntu12.04文档:techotopia ,ruby文档,the hard way learn ...
- 看来System.dll是没法剔除依赖了
今天花了半天时间将System.Xml换成了Mono.Xml 想干掉System.dll发现不行了,System.Net以及System.IO都在这下面,还有protobuf-net也逃不掉这个 算啦 ...