[简单日记]服务层通用post请求
来源:互联网 发布:孙鑫java视频教程 编辑:程序博客网 时间:2024/05/22 06:33
/**
* 使用post请求
* @param obj 请求参数对象
* @param httpUrl 请求URL地址
* @return 请求网关的返回值
* @throws Exception
*/
private String executePost(Object obj, String httpUrl) throws Exception {
String result = "";
Class cls = obj.getClass();
Field[] fields = cls.getDeclaredFields();
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
//设置请求参数
String fieldName = null;
String fieldNameUpper = null;
Method getMethod = null;
String value = null;
for (int i = 0; i < fields.length; i++) {//循环设置请求参数
fieldName = fields[i].getName();
fieldNameUpper = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
getMethod = cls.getMethod("get" + fieldNameUpper);//通过反射获取get方法
value = (String) getMethod.invoke(obj);//通过反射调用get方法
if(value != null) {//请求参数值不为空,才设置
params.add(new BasicNameValuePair(fieldName, value));
}
}
HttpPost httppost = new HttpPost(httpUrl);//设置HttpPost
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 设置参数的编码UTF-8
HttpClient httpclient = new DefaultHttpClient();//创建HttpClient
HttpEntity entity = httpclient.execute(httppost).getEntity();//Http请求网关
if(entity != null&& entity.getContentLength() >0) {//返回值不为空,且长度大于0
result=EntityUtils.toString(entity);//将返回值转换成字符串
}//处理返回结果
httpclient.getConnectionManager().shutdown();//关闭连接
r eturn result;//返回返回值
}
* 使用post请求
* @param obj 请求参数对象
* @param httpUrl 请求URL地址
* @return 请求网关的返回值
* @throws Exception
*/
private String executePost(Object obj, String httpUrl) throws Exception {
String result = "";
Class cls = obj.getClass();
Field[] fields = cls.getDeclaredFields();
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
//设置请求参数
String fieldName = null;
String fieldNameUpper = null;
Method getMethod = null;
String value = null;
for (int i = 0; i < fields.length; i++) {//循环设置请求参数
fieldName = fields[i].getName();
fieldNameUpper = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
getMethod = cls.getMethod("get" + fieldNameUpper);//通过反射获取get方法
value = (String) getMethod.invoke(obj);//通过反射调用get方法
if(value != null) {//请求参数值不为空,才设置
params.add(new BasicNameValuePair(fieldName, value));
}
}
HttpPost httppost = new HttpPost(httpUrl);//设置HttpPost
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 设置参数的编码UTF-8
HttpClient httpclient = new DefaultHttpClient();//创建HttpClient
HttpEntity entity = httpclient.execute(httppost).getEntity();//Http请求网关
if(entity != null&& entity.getContentLength() >0) {//返回值不为空,且长度大于0
result=EntityUtils.toString(entity);//将返回值转换成字符串
}//处理返回结果
httpclient.getConnectionManager().shutdown();//关闭连接
r eturn result;//返回返回值
}
阅读全文
0 0
- [简单日记]服务层通用post请求
- 简单post请求
- 简单通用数据访问层
- 简单网络请求/GET请求/POST请求
- Java发送post,get请求通用方法
- GET POST通用的网络请求
- Okhttp post请求简单使用
- 简单的POST请求和get请求
- 发送http post请求soap服务
- 发送http post请求soap服务
- 发送http post请求soap服务
- Ionic2 Post请求后台服务数据
- 简单通用数据访问映射层
- IOS之同步请求、异步请求、GET请求、POST请求-日记
- Android 网络请求通用的get与post方式
- 向通用块层提交一个bio请求
- C++ 简单实现HTTP GET/POST 请求
- ASIHTTPRequest 对GET POST 请求简单封装
- Linux根目录下重要目录的作用
- django 分页效果实现(djangorestframework内置以及django内置方法)
- 有趣有料的英语
- HTML5 初始之旅
- 在数组中寻找连续子数组和最大值
- [简单日记]服务层通用post请求
- nodejs连接池连接mysql数据库
- 机器学习模型训练测试完整步骤
- AIDL使用详解(一)
- shuffle函数打乱(images, labels)
- mongodb实战之查询
- thymeleaf介绍
- ElasticSearch数组对象,以及Kibana相关操作
- PHP mysqli_real_escape_string()函数--转义 SQL 语句中的特殊字符