公共代码参考(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 ...
随机推荐
- en_windows_10_multiple_editions_version_1511_x64.iso
好久没折腾电脑了,这几天在E盘装了个64位Windows 10 TH2 专业版,从MSDN官网下载的英文原版镜像,用kms10未能激活,一看日志文件,说我这是零售版,后面就关掉了什么监听端口,然后就完 ...
- C++使用继承时子对象的内存布局
C++使用继承时子对象的内存布局 // */ // ]]> C++使用继承时子对象的内存布局 Table of Contents 1 示例程序 2 对象的内存布局 1 示例程序 class ...
- 字符串对象-String
新建字符串对象 ① 直接赋值 ② 构造函数 ③ 转换函数 1 length 字符串对象属性 2 match() null 跟php中的preg_matc ...
- iOS:xCode7版本运行xCode8.0的代码
怎么在xCode7版本上运行xCode8.0的代码? 1.右键你的"LaunchScreen.sb"文件并用编辑器打开sb 2.删掉"<capability nam ...
- C++实现简单的单链表
下面实现的是一个简单的单链表 功能不多,学习使用 #pragma once #include <iostream> using namespace std; class ListEx { ...
- css3之文本新属性
- Android Studio Gradle 问题 解决方案
将AS2.0升到2.1.3出现的问题: Error:No service of type Factory available in ProjectScopeServices. 在项目中找到build ...
- 猿团YTFCloud--5分钟自制APP,开发从未如此简单
9月15日,YTFCloud将正式开启内测, 这意味着猿团YTF框架产品线全面升级.同时,公测过后,YTFCloud的APP线上DIY服务将面向所有用户,让人人都能成为APP“开发商”. 什么是YTF ...
- 服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
转载的服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
- UI进阶
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...