增加对纯文本方式HTTP请求的支持

优化部分error日志
优化部分方法的注解
This commit is contained in:
seagull 2018-08-13 10:16:53 +08:00
parent b7a4bfa6b9
commit b7029ffcf8
7 changed files with 490 additions and 166 deletions

View File

@ -323,7 +323,7 @@ public class EncapsulateOperation {
LogUtil.APP.info("执行JS...【" + operationValue + "】,返回的结果为:" + result); LogUtil.APP.info("执行JS...【" + operationValue + "】,返回的结果为:" + result);
} else { } else {
result = "执行JS...【" + operationValue + ""; result = "执行JS...【" + operationValue + "";
LogUtil.APP.info(result); LogUtil.APP.info(result+"执行JS返回null或没有返回");
} }
break; break;
case "gotodefaultcontent": case "gotodefaultcontent":

View File

@ -42,7 +42,7 @@ public class WebDriverInitialization{
System.setProperty("webdriver.ie.driver",drivenpath+"IEDriverServer.exe"); System.setProperty("webdriver.ie.driver",drivenpath+"IEDriverServer.exe");
webDriver = new InternetExplorerDriver(); webDriver = new InternetExplorerDriver();
}else{ }else{
luckyclient.publicclass.LogUtil.ERROR.info("当前操作系统无法进行IE浏览器的Web UI测试请选择火狐或是谷歌浏览器"); luckyclient.publicclass.LogUtil.APP.error("当前操作系统无法进行IE浏览器的Web UI测试请选择火狐或是谷歌浏览器");
} }
}else if(drivertype==1){ }else if(drivertype==1){
if(os.startsWith("win")){ if(os.startsWith("win")){
@ -67,11 +67,11 @@ public class WebDriverInitialization{
System.setProperty("webdriver.edge.driver",drivenpath+"MicrosoftWebDriver.exe"); System.setProperty("webdriver.edge.driver",drivenpath+"MicrosoftWebDriver.exe");
webDriver = new EdgeDriver(); webDriver = new EdgeDriver();
}else{ }else{
luckyclient.publicclass.LogUtil.ERROR.info("当前操作系统无法进行Edge浏览器的Web UI测试请选择火狐或是谷歌浏览器"); luckyclient.publicclass.LogUtil.APP.error("当前操作系统无法进行Edge浏览器的Web UI测试请选择火狐或是谷歌浏览器");
} }
}else{ }else{
luckyclient.publicclass.LogUtil.ERROR.info("浏览器类型标识:"+drivertype); luckyclient.publicclass.LogUtil.APP.error("浏览器类型标识:"+drivertype);
luckyclient.publicclass.LogUtil.ERROR.info("获取到的浏览器类型标识未定义默认IE浏览器进行执行...."); luckyclient.publicclass.LogUtil.APP.error("获取到的浏览器类型标识未定义默认IE浏览器进行执行....");
System.setProperty("webdriver.ie.driver",drivenpath+"IEDriverServer.exe"); System.setProperty("webdriver.ie.driver",drivenpath+"IEDriverServer.exe");
webDriver = new InternetExplorerDriver(); webDriver = new InternetExplorerDriver();
} }

View File

@ -79,11 +79,11 @@ public class WebDriverAnalyticCase {
caselog.caseLogDetail(taskid, projectcase.getSign(),"步骤编号:"+step.getStepnum()+" 解析自动化用例步骤脚本完成!","info",String.valueOf(step.getStepnum()),""); caselog.caseLogDetail(taskid, projectcase.getSign(),"步骤编号:"+step.getStepnum()+" 解析自动化用例步骤脚本完成!","info",String.valueOf(step.getStepnum()),"");
} }
}catch(Exception e) { }catch(Exception e) {
luckyclient.publicclass.LogUtil.ERROR.error("用例编号:"+projectcase.getSign()+" 步骤编号:"+step.getStepnum()+" 解析自动化用例步骤脚本出错!"); luckyclient.publicclass.LogUtil.APP.error("用例编号:"+projectcase.getSign()+" 步骤编号:"+step.getStepnum()+" 解析自动化用例步骤脚本出错!");
if(null!=caselog){ if(null!=caselog){
caselog.caseLogDetail(taskid, projectcase.getSign(),"步骤编号:"+step.getStepnum()+" 解析自动化用例步骤脚本出错!","error",String.valueOf(step.getStepnum()),""); caselog.caseLogDetail(taskid, projectcase.getSign(),"步骤编号:"+step.getStepnum()+" 解析自动化用例步骤脚本出错!","error",String.valueOf(step.getStepnum()),"");
} }
luckyclient.publicclass.LogUtil.ERROR.error(e,e); luckyclient.publicclass.LogUtil.APP.error(e,e);
params.put("exception","用例编号:"+projectcase.getSign()+"|解析异常,用例步骤为空或是用例脚本错误!"); params.put("exception","用例编号:"+projectcase.getSign()+"|解析异常,用例步骤为空或是用例脚本错误!");
return params; return params;
} }

View File

@ -1,8 +1,13 @@
package luckyclient.publicclass; package luckyclient.publicclass;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/** /**
* ================================================================= * =================================================================
* 这是一个受限制的自由软件您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途也不允许对程序代码修改后以任何形式任何目的的再发布 * 这是一个受限制的自由软件您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途也不允许对程序代码修改后以任何形式任何目的的再发布
@ -111,6 +116,84 @@ public class ChangString {
return pattern.matcher(str).matches(); return pattern.matcher(str).matches();
} }
/**
* 替换变量类型
* @param object
* @param str
* @return
*/
public static Object settype(Object object,String str){
if(object instanceof Integer){
return Integer.valueOf(str);
}else if(object instanceof Boolean){
return Boolean.valueOf(str);
}else if(object instanceof Long){
return Long.valueOf(str);
}else if(object instanceof Timestamp){
return Timestamp.valueOf(str);
}else if(object instanceof JSONObject){
return JSONObject.parseObject(str);
}else if(object instanceof JSONArray){
return JSONArray.parseArray(str);
}else{
return str;
}
}
/**
* 替换json中的变量
* @param json
* @param key
* @param value
* @return
*/
public static Map<String,String> changjson(String json, String key, String value) {
Map<String,String> map=new HashMap<String,String>(0);
map.put("boolean", "false");
map.put("json", json);
if (json.startsWith("{") && json.endsWith("}")) {
try {
JSONObject jsonStr = JSONObject.parseObject(json);
if(jsonStr.containsKey(key)){
jsonStr.put(key, settype(jsonStr.get(key),value));
map.put("boolean", "true");
map.put("json", jsonStr.toJSONString());
luckyclient.publicclass.LogUtil.APP.info("JSON字符串替换成功原始JSON:【"+json+"】 新JSON:【"+jsonStr.toJSONString()+"");
}
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error("格式化成JSON异常请检查参数"+json,e);
return map;
}
} else if (json.startsWith("[") && json.endsWith("]")) {
try {
JSONArray jsonarr = JSONArray.parseArray(json);
JSONObject jsonStr=new JSONObject();
int index=0;
if(key.indexOf("[")>=0 && key.endsWith("]")){
index=Integer.valueOf(key.substring(key.lastIndexOf("[")+1,key.lastIndexOf("]")));
key=key.substring(0, key.lastIndexOf("["));
jsonStr = jsonarr.getJSONObject(index);
luckyclient.publicclass.LogUtil.APP.info("准备替换JSONArray中的参数值未检测到指定参数名序号默认替换第1个参数...");
}else{
jsonStr = jsonarr.getJSONObject(index);
luckyclient.publicclass.LogUtil.APP.info("准备替换JSONArray中的参数值替换指定第"+index+"个参数...");
}
if(jsonStr.containsKey(key)){
jsonStr.put(key, settype(jsonStr.get(key),value));
jsonarr.set(index, jsonStr);
map.put("boolean", "true");
map.put("json", jsonarr.toJSONString());
}
luckyclient.publicclass.LogUtil.APP.info("JSONARRAY字符串替换成功原始JSONARRAY:【"+json+"】 新JSONARRAY:【"+jsonarr.toJSONString()+"");
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error("格式化成JSONArray异常请检查参数"+json,e);
return map;
}
}
return map;
}
public static void main(String[] args) { public static void main(String[] args) {
} }

View File

@ -61,10 +61,10 @@ public class InvokeMethod {
} else if (steptype == 2) { } else if (steptype == 2) {
String templateidstr = action.substring(1, action.indexOf("")); String templateidstr = action.substring(1, action.indexOf(""));
String templatenamestr = action.substring(action.indexOf("") + 1); String templatenamestr = action.substring(action.indexOf("") + 1);
luckyclient.publicclass.LogUtil.APP.info("即将使用模板【" + templatenamestr + "ID" + templateidstr + "】发送HTTP请求"); luckyclient.publicclass.LogUtil.APP.info("即将使用模板【" + templatenamestr + "ID:" + templateidstr + "】发送HTTP请求");
String httpppt = HttpRequest.loadJSON("/projectprotocolTemplate/cgetPTemplateById.do?templateid=" + templateidstr); String httpppt = HttpRequest.loadJSON("/projectprotocolTemplate/cgetPTemplateById.do?templateid=" + templateidstr);
ProjectProtocolTemplate ppt = JSONObject.parseObject(httpppt, ProjectProtocolTemplate.class); ProjectProtocolTemplate ppt = JSONObject.parseObject(httpppt,ProjectProtocolTemplate.class);
if (null == ppt) { if (null == ppt) {
luckyclient.publicclass.LogUtil.APP.error("协议模板为空,请检查用例使用的协议模板是否已经删除!"); luckyclient.publicclass.LogUtil.APP.error("协议模板为空,请检查用例使用的协议模板是否已经删除!");
return "协议模板为空,请确认用例使用的模板是否已经删除!"; return "协议模板为空,请确认用例使用的模板是否已经删除!";
@ -76,7 +76,7 @@ public class InvokeMethod {
paramslist = JSONObject.parseArray(jsonptpObject.getString("params"), ProjectTemplateParams.class); paramslist = JSONObject.parseArray(jsonptpObject.getString("params"), ProjectTemplateParams.class);
//处理头域 //处理头域
Map<String, String> headmsg = new HashMap<>(0); Map<String, String> headmsg = new HashMap<String, String>(0);
if (null != ppt.getHeadmsg() && !ppt.getHeadmsg().equals("") && ppt.getHeadmsg().indexOf("=") > 0) { if (null != ppt.getHeadmsg() && !ppt.getHeadmsg().equals("") && ppt.getHeadmsg().indexOf("=") > 0) {
String headmsgtemp = ppt.getHeadmsg().replace("\\;", "!!!fhzh"); String headmsgtemp = ppt.getHeadmsg().replace("\\;", "!!!fhzh");
String[] temp = headmsgtemp.split(";", -1); String[] temp = headmsgtemp.split(";", -1);
@ -107,11 +107,33 @@ public class InvokeMethod {
int replaceflag=0; int replaceflag=0;
for (int i = 0; i < paramslist.size(); i++) { for (int i = 0; i < paramslist.size(); i++) {
ProjectTemplateParams ptp = paramslist.get(i); ProjectTemplateParams ptp = paramslist.get(i);
if (ptp.getParamname().equals(key)) { if("_forTextJson".equals(ptp.getParamname())){
ptp.setParam(value); if(ptp.getParam().indexOf("\""+key+"\":")>=0){
paramslist.set(i, ptp); Map<String,String> map=ChangString.changjson(ptp.getParam(), key, value);
replaceflag=1; if("true".equals(map.get("boolean"))){
break; ptp.setParam(map.get("json"));
paramslist.set(i, ptp);
replaceflag=1;
luckyclient.publicclass.LogUtil.APP.info("替换参数"+key+"完成...");
break;
}
}else if(ptp.getParam().indexOf(key)>=0){
ptp.setParam(ptp.getParam().replace(key, value));
paramslist.set(i, ptp);
replaceflag=1;
luckyclient.publicclass.LogUtil.APP.info("检查当前文本不属于JSON,在字符串【"+ptp.getParam()+"】中直接把【"+key+"】替换成【"+value+"】...");
break;
}else{
luckyclient.publicclass.LogUtil.APP.error("请检查您的纯文本模板是否是正常的JSON格式或是文本中是否存在需替换的关键字。");
}
}else{
if (ptp.getParamname().equals(key)) {
ptp.setParam(value);
paramslist.set(i, ptp);
replaceflag=1;
luckyclient.publicclass.LogUtil.APP.info("把模板中参数【"+key+"】的值设置成【"+value+"");
break;
}
} }
} }
if(replaceflag==0){ if(replaceflag==0){
@ -193,11 +215,15 @@ public class InvokeMethod {
} else if (steptype == 3) { } else if (steptype == 3) {
String templateidstr = action.substring(1, action.indexOf("")); String templateidstr = action.substring(1, action.indexOf(""));
String templatenamestr = action.substring(action.indexOf("") + 1); String templatenamestr = action.substring(action.indexOf("") + 1);
luckyclient.publicclass.LogUtil.APP.info("即将使用模板【" + templatenamestr + " ID:【" + templateidstr + "】 发送SOCKET请求"); luckyclient.publicclass.LogUtil.APP.info("即将使用模板【" + templatenamestr + "ID:【" + templateidstr + "】 发送SOCKET请求");
String httpppt = HttpRequest.loadJSON("/projectprotocolTemplate/cgetPTemplateById.do?templateid=" + templateidstr); String httpppt = HttpRequest.loadJSON("/projectprotocolTemplate/cgetPTemplateById.do?templateid=" + templateidstr);
ProjectProtocolTemplate ppt = JSONObject.parseObject(httpppt,ProjectProtocolTemplate.class); ProjectProtocolTemplate ppt = JSONObject.parseObject(httpppt,ProjectProtocolTemplate.class);
if (null == ppt) {
luckyclient.publicclass.LogUtil.APP.error("协议模板为空,请检查用例使用的协议模板是否已经删除!");
return "协议模板为空,请确认用例使用的模板是否已经删除!";
}
String httpptp = HttpRequest.loadJSON("/projectTemplateParams/cgetParamsByTemplate.do?templateid=" + templateidstr); String httpptp = HttpRequest.loadJSON("/projectTemplateParams/cgetParamsByTemplate.do?templateid=" + templateidstr);
JSONObject jsonptpObject = JSONObject.parseObject(httpptp); JSONObject jsonptpObject = JSONObject.parseObject(httpptp);
List<ProjectTemplateParams> paramslist = new ArrayList<ProjectTemplateParams>(); List<ProjectTemplateParams> paramslist = new ArrayList<ProjectTemplateParams>();
@ -224,22 +250,58 @@ public class InvokeMethod {
String msgend = ")"; String msgend = ")";
for (Object obp : getParameterValues) { for (Object obp : getParameterValues) {
String paramob = obp.toString(); String paramob = obp.toString();
String key = paramob.substring(0, paramob.indexOf("#")); if(paramob.contains("#")){
String value = paramob.substring(paramob.indexOf("#") + 1); String key = paramob.substring(0, paramob.indexOf("#"));
if (key.contains(booleanheadmsg) && key.contains(msgend)) { String value = paramob.substring(paramob.indexOf("#") + 1);
String head = key.substring(key.indexOf(booleanheadmsg) + 8, key.lastIndexOf(msgend)); if (key.contains(booleanheadmsg) && key.contains(msgend)) {
headmsg.put(head, value); String head = key.substring(key.indexOf(booleanheadmsg) + 8, key.lastIndexOf(msgend));
continue; headmsg.put(head, value);
} continue;
for (int i = 0; i < paramslist.size(); i++) {
ProjectTemplateParams ptp = paramslist.get(i);
if (ptp.getParamname().equals(key)) {
ptp.setParam(value);
paramslist.set(i, ptp);
} }
int replaceflag=0;
for (int i = 0; i < paramslist.size(); i++) {
ProjectTemplateParams ptp = paramslist.get(i);
if("_forTextJson".equals(ptp.getParamname())){
if(ptp.getParam().indexOf("\""+key+"\":")>=0){
Map<String,String> map=ChangString.changjson(ptp.getParam(), key, value);
if("true".equals(map.get("boolean"))){
ptp.setParam(map.get("json"));
paramslist.set(i, ptp);
replaceflag=1;
luckyclient.publicclass.LogUtil.APP.info("替换参数"+key+"完成...");
break;
}
}else if(ptp.getParam().indexOf(key)>=0){
ptp.setParam(ptp.getParam().replace(key, value));
paramslist.set(i, ptp);
replaceflag=1;
luckyclient.publicclass.LogUtil.APP.info("检查当前文本不属于JSON,在字符串【"+ptp.getParam()+"】中直接把【"+key+"】替换成【"+value+"】...");
break;
}else{
luckyclient.publicclass.LogUtil.APP.error("请检查您的纯文本模板是否是正常的JSON格式或是文本中是否存在需替换的关键字。");
}
}else{
if (ptp.getParamname().equals(key)) {
ptp.setParam(value);
paramslist.set(i, ptp);
replaceflag=1;
luckyclient.publicclass.LogUtil.APP.info("把模板中参数【"+key+"】的值设置成【"+value+"");
break;
}
}
}
if(replaceflag==0){
luckyclient.publicclass.LogUtil.APP.error("步骤参数【"+key+"】没有在模板中找到可替换的参数对应默认值,"
+ "设置请求参数失败,请检查协议模板中此参数是否存在。");
}
}else{
luckyclient.publicclass.LogUtil.APP.error("替换模板或是头域参数失败,原因是因为没有检测到#"
+ "注意HTTP请求替换参数格式是【headmsg(头域名#头域值)|参数名#参数值|参数名2#参数值2】");
} }
} }
} }
//处理参数
Map<String, Object> params = new HashMap<String, Object>(0); Map<String, Object> params = new HashMap<String, Object>(0);
for (ProjectTemplateParams ptp : paramslist) { for (ProjectTemplateParams ptp : paramslist) {
//处理参数对象 //处理参数对象
@ -247,16 +309,30 @@ public class InvokeMethod {
String tempparam = ptp.getParam().replace("&quot;", "\""); String tempparam = ptp.getParam().replace("&quot;", "\"");
JSONObject json = JSONObject.parseObject(tempparam); JSONObject json = JSONObject.parseObject(tempparam);
params.put(ptp.getParamname().replace("&quot;", "\""), json); params.put(ptp.getParamname().replace("&quot;", "\""), json);
luckyclient.publicclass.LogUtil.APP.info("模板参数【" + ptp.getParamname() + "】 JSONObject类型参数值:【" + json.toString() + "");
} else if (ptp.getParamtype() == 2) { } else if (ptp.getParamtype() == 2) {
String tempparam = ptp.getParam().replace("&quot;", "\""); String tempparam = ptp.getParam().replace("&quot;", "\"");
JSONArray jarr = JSONArray.parseArray(tempparam); JSONArray jarr = JSONArray.parseArray(tempparam);
params.put(ptp.getParamname().replace("&quot;", "\""), jarr); params.put(ptp.getParamname().replace("&quot;", "\""), jarr);
luckyclient.publicclass.LogUtil.APP.info("模板参数【" + ptp.getParamname() + "】 JSONArray类型参数值:【" + jarr.toString() + "");
} else if (ptp.getParamtype() == 3) { } else if (ptp.getParamtype() == 3) {
String tempparam = ptp.getParam().replace("&quot;", "\""); String tempparam = ptp.getParam().replace("&quot;", "\"");
File file = new File(tempparam); File file = new File(tempparam);
params.put(ptp.getParamname().replace("&quot;", "\""), file); params.put(ptp.getParamname().replace("&quot;", "\""), file);
luckyclient.publicclass.LogUtil.APP.info("模板参数【" + ptp.getParamname() + "】 File类型参数值:【" + file.getAbsolutePath() + "");
} else if (ptp.getParamtype() == 4) {
String tempparam = ptp.getParam().replace("&quot;", "\"");
Double dp = Double.valueOf(tempparam);
params.put(ptp.getParamname().replace("&quot;", "\""), dp);
luckyclient.publicclass.LogUtil.APP.info("模板参数【" + ptp.getParamname() + "】 数字类型参数值:【" + tempparam + "");
} else if (ptp.getParamtype() == 5) {
String tempparam = ptp.getParam().replace("&quot;", "\"");
Boolean bn = Boolean.valueOf(tempparam);
params.put(ptp.getParamname().replace("&quot;", "\""), bn);
luckyclient.publicclass.LogUtil.APP.info("模板参数【" + ptp.getParamname() + "】 Boolean类型参数值:【" + bn + "");
} else { } else {
params.put(ptp.getParamname().replace("&quot;", "\""), ptp.getParam().replace("&quot;", "\"")); params.put(ptp.getParamname().replace("&quot;", "\""), ptp.getParam().replace("&quot;", "\""));
luckyclient.publicclass.LogUtil.APP.info("模板参数【" + ptp.getParamname() + "】 String类型参数值:【" + ptp.getParam().replace("&quot;", "\"") + "");
} }
} }

View File

@ -65,7 +65,13 @@ import com.alibaba.fastjson.JSON;
*/ */
public class HttpClientHelper { public class HttpClientHelper {
/** /**
* @Description:使用HttpURLConnection发送post请求 * 使用HttpURLConnection发送post请求
* @param urlParam
* @param params
* @param charset
* @param timeout
* @param headmsg
* @return
*/ */
public static String sendHttpURLPost(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) { public static String sendHttpURLPost(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -73,12 +79,17 @@ public class HttpClientHelper {
// 构建请求参数 // 构建请求参数
StringBuffer sbParams = new StringBuffer(); StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) { if (params != null && params.size() > 0) {
for (Entry<String, Object> e : params.entrySet()) { if(1==params.size()&&params.containsKey("_forTextJson")){
sbParams.append(e.getKey()); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendHttpURLPost请求(必须为key-value)...");
sbParams.append("="); return "协议模板是纯文本无法使用sendHttpURLPost请求(必须为key-value)...";
sbParams.append(e.getValue()); }else{
sbParams.append("&"); for (Entry<String, Object> e : params.entrySet()) {
luckyclient.publicclass.LogUtil.APP.info("设置HTTPURLPost参数信息...key:【"+e.getKey()+"】 value:【"+e.getValue()+""); sbParams.append(e.getKey());
sbParams.append("=");
sbParams.append(e.getValue());
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置HTTPURLPost参数信息...key:【"+e.getKey()+"】 value:【"+e.getValue()+"");
}
} }
} }
HttpURLConnection con = null; HttpURLConnection con = null;
@ -167,7 +178,13 @@ public class HttpClientHelper {
} }
/** /**
* @Description:使用URLConnection发送post * 使用URLConnection发送post
* @param urlParam
* @param params
* @param charset
* @param timeout
* @param headmsg
* @return
*/ */
public static String sendURLPost(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) { public static String sendURLPost(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -175,12 +192,17 @@ public class HttpClientHelper {
luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+""); luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+"");
StringBuffer sbParams = new StringBuffer(); StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) { if (params != null && params.size() > 0) {
for (Entry<String, Object> e : params.entrySet()) { if(1==params.size()&&params.containsKey("_forTextJson")){
sbParams.append(e.getKey()); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendURLPost请求(必须为key-value)...");
sbParams.append("="); return "协议模板是纯文本无法使用sendURLPost请求(必须为key-value)...";
sbParams.append(e.getValue()); }else{
sbParams.append("&"); for (Entry<String, Object> e : params.entrySet()) {
luckyclient.publicclass.LogUtil.APP.info("设置URLPost参数信息...key:【"+e.getKey()+"】 value:【"+e.getValue()+""); sbParams.append(e.getKey());
sbParams.append("=");
sbParams.append(e.getValue());
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置URLPost参数信息...key:【"+e.getKey()+"】 value:【"+e.getValue()+"");
}
} }
} }
URLConnection con = null; URLConnection con = null;
@ -264,19 +286,28 @@ public class HttpClientHelper {
} }
/** /**
* @Description:发送get请求保存下载文件 * 发送get请求保存下载文件
* @param urlParam
* @param params
* @param fileSavePath
* @param timeout
* @param headmsg
*/ */
public static void sendGetAndSaveFile(String urlParam, Map<String, Object> params, String fileSavePath, int timeout,Map<String, String> headmsg) { public static void sendGetAndSaveFile(String urlParam, Map<String, Object> params, String fileSavePath, int timeout,Map<String, String> headmsg) {
// 构建请求参数 // 构建请求参数
luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+""); luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+"");
StringBuffer sbParams = new StringBuffer(); StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) { if (params != null && params.size() > 0) {
for (Entry<String, Object> entry : params.entrySet()) { if(1==params.size()&&params.containsKey("_forTextJson")){
sbParams.append(entry.getKey()); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendGetAndSaveFile请求(必须为key-value)...");
sbParams.append("="); }else{
sbParams.append(entry.getValue()); for (Entry<String, Object> entry : params.entrySet()) {
sbParams.append("&"); sbParams.append(entry.getKey());
luckyclient.publicclass.LogUtil.APP.info("设置HTTPSaveFile参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+""); sbParams.append("=");
sbParams.append(entry.getValue());
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置HTTPSaveFile参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+"");
}
} }
} }
HttpURLConnection con = null; HttpURLConnection con = null;
@ -351,7 +382,13 @@ public class HttpClientHelper {
} }
/** /**
* @Description:使用HttpURLConnection发送get请求 * 使用HttpURLConnection发送get请求
* @param urlParam
* @param params
* @param charset
* @param timeout
* @param headmsg
* @return
*/ */
public static String sendHttpURLGet(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) { public static String sendHttpURLGet(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -359,12 +396,17 @@ public class HttpClientHelper {
luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+""); luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+"");
StringBuffer sbParams = new StringBuffer(); StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) { if (params != null && params.size() > 0) {
for (Entry<String, Object> entry : params.entrySet()) { if(1==params.size()&&params.containsKey("_forTextJson")){
sbParams.append(entry.getKey()); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendHttpURLGet请求(必须为key-value)...");
sbParams.append("="); return "协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendHttpURLGet请求(必须为key-value)...";
sbParams.append(entry.getValue()); }else{
sbParams.append("&"); for (Entry<String, Object> entry : params.entrySet()) {
luckyclient.publicclass.LogUtil.APP.info("设置HTTPURLGet参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+""); sbParams.append(entry.getKey());
sbParams.append("=");
sbParams.append(entry.getValue());
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置HTTPURLGet参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+"");
}
} }
} }
HttpURLConnection con = null; HttpURLConnection con = null;
@ -426,7 +468,13 @@ public class HttpClientHelper {
} }
/** /**
* @Description:使用URLConnection发送get请求 * 使用URLConnection发送get请求
* @param urlParam
* @param params
* @param charset
* @param timeout
* @param headmsg
* @return
*/ */
public static String sendURLGet(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) { public static String sendURLGet(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -434,13 +482,19 @@ public class HttpClientHelper {
luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+""); luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+"");
StringBuffer sbParams = new StringBuffer(); StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) { if (params != null && params.size() > 0) {
for (Entry<String, Object> entry : params.entrySet()) { if(1==params.size()&&params.containsKey("_forTextJson")){
sbParams.append(entry.getKey()); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendURLGet请求(必须为key-value)...");
sbParams.append("="); return "协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendURLGet请求(必须为key-value)...";
sbParams.append(entry.getValue()); }else{
sbParams.append("&"); for (Entry<String, Object> entry : params.entrySet()) {
luckyclient.publicclass.LogUtil.APP.info("设置URLGet参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+""); sbParams.append(entry.getKey());
sbParams.append("=");
sbParams.append(entry.getValue());
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置URLGet参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+"");
}
} }
} }
BufferedReader br = null; BufferedReader br = null;
try { try {
@ -500,9 +554,15 @@ public class HttpClientHelper {
} }
/** /**
* @throws NoSuchAlgorithmException * 使用HttpClient以JSON格式发送post请求
* @throws KeyManagementException * @param urlParam
* @Description:使用HttpClient以JSON格式发送post请求 * @param params
* @param charset
* @param headmsg
* @param cerpath
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/ */
public static String httpClientPostJson(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws NoSuchAlgorithmException, KeyManagementException { public static String httpClientPostJson(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws NoSuchAlgorithmException, KeyManagementException {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -528,11 +588,17 @@ public class HttpClientHelper {
BufferedReader br = null; BufferedReader br = null;
try { try {
if(params.size()>0){ if(params.size()>0){
if(1==params.size()&&params.containsKey("_forTextJson")){
luckyclient.publicclass.LogUtil.APP.info("参数类型TEXT,设置HTTPPostJson参数信息...【"+params.get("_forTextJson").toString()+"");
StringEntity entity = new StringEntity(params.get("_forTextJson").toString(),charset);
httpPost.setEntity(entity);
}else{
String jsonString = JSON.toJSONString(params); String jsonString = JSON.toJSONString(params);
luckyclient.publicclass.LogUtil.APP.info("设置HTTPPostJson参数信息...【"+jsonString+""); luckyclient.publicclass.LogUtil.APP.info("参数类型FORM,设置HTTPPostJson参数信息...【"+jsonString+"");
StringEntity entity = new StringEntity(jsonString,charset); StringEntity entity = new StringEntity(jsonString,charset);
httpPost.setEntity(entity); httpPost.setEntity(entity);
} }
}
CloseableHttpResponse response = httpclient.execute(httpPost); CloseableHttpResponse response = httpclient.execute(httpPost);
@ -566,9 +632,15 @@ public class HttpClientHelper {
} }
/** /**
* @throws NoSuchAlgorithmException * 使用HttpClient发送post请求
* @throws KeyManagementException * @param urlParam
* @Description:使用HttpClient发送post请求 * @param params
* @param charset
* @param headmsg
* @param cerpath
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/ */
public static String httpClientPost(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws NoSuchAlgorithmException, KeyManagementException { public static String httpClientPost(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws NoSuchAlgorithmException, KeyManagementException {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -593,13 +665,18 @@ public class HttpClientHelper {
BufferedReader br = null; BufferedReader br = null;
try { try {
if(params.size()>0){ if(params.size()>0){
//拼接参数 if(1==params.size()&&params.containsKey("_forTextJson")){
List <NameValuePair> nvps = new ArrayList <NameValuePair>(); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用httpClientPost请求(必须为key-value)...");
for (Map.Entry<String, Object> m :params.entrySet()) { return "协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用httpClientPost请求(必须为key-value)...";
nvps.add(new BasicNameValuePair(m.getKey(), m.getValue().toString())); }else{
luckyclient.publicclass.LogUtil.APP.info("设置HTTPClientPost参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+""); //拼接参数
} List <NameValuePair> nvps = new ArrayList <NameValuePair>();
httpPost.setEntity(new UrlEncodedFormEntity(nvps,charset)); for (Map.Entry<String, Object> m :params.entrySet()) {
nvps.add(new BasicNameValuePair(m.getKey(), m.getValue().toString()));
luckyclient.publicclass.LogUtil.APP.info("设置HTTPClientPost参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+"");
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps,charset));
}
} }
CloseableHttpResponse response = httpclient.execute(httpPost); CloseableHttpResponse response = httpclient.execute(httpPost);
@ -635,9 +712,15 @@ public class HttpClientHelper {
} }
/** /**
* @throws NoSuchAlgorithmException * 使用HttpClient上传文件
* @throws KeyManagementException * @param urlParam
* @Description:使用HttpClient上传文件 * @param params
* @param charset
* @param headmsg
* @param cerpath
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/ */
public static String httpClientUploadFile(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws NoSuchAlgorithmException, KeyManagementException { public static String httpClientUploadFile(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws NoSuchAlgorithmException, KeyManagementException {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -662,22 +745,27 @@ public class HttpClientHelper {
BufferedReader br = null; BufferedReader br = null;
try { try {
if(params.size()>0){ if(params.size()>0){
//拼接参数 if(1==params.size()&&params.containsKey("_forTextJson")){
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用httpClientUploadFile请求(必须为key-value)...");
//设置请求的编码格式 return "协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用httpClientUploadFile请求(必须为key-value)...";
entityBuilder.setCharset(Charset.forName(charset)); }else{
//拼接参数
for (Map.Entry<String, Object> m :params.entrySet()) { MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
if (m.getValue() instanceof File) { //设置请求的编码格式
entityBuilder.addBinaryBody(m.getKey(), (File)m.getValue()); entityBuilder.setCharset(Charset.forName(charset));
luckyclient.publicclass.LogUtil.APP.info("设置httpClientUploadFile 上传文件参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+"");
}else{ for (Map.Entry<String, Object> m :params.entrySet()) {
entityBuilder.addTextBody(m.getKey(), m.getValue().toString()); if (m.getValue() instanceof File) {
luckyclient.publicclass.LogUtil.APP.info("设置httpClientUploadFile参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+""); entityBuilder.addBinaryBody(m.getKey(), (File)m.getValue());
} luckyclient.publicclass.LogUtil.APP.info("设置httpClientUploadFile 上传文件参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+"");
} }else{
HttpEntity reqEntity =entityBuilder.build(); entityBuilder.addTextBody(m.getKey(), m.getValue().toString());
httpPost.setEntity(reqEntity); luckyclient.publicclass.LogUtil.APP.info("设置httpClientUploadFile参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+"");
}
}
HttpEntity reqEntity =entityBuilder.build();
httpPost.setEntity(reqEntity);
}
} }
CloseableHttpResponse response = httpclient.execute(httpPost); CloseableHttpResponse response = httpclient.execute(httpPost);
@ -712,9 +800,15 @@ public class HttpClientHelper {
} }
/** /**
* @throws NoSuchAlgorithmException * 使用HttpClient发送get请求
* @throws KeyManagementException * @param urlParam
* @Description:使用HttpClient发送get请求 * @param params
* @param charset
* @param headmsg
* @param cerpath
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/ */
public static String httpClientGet(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws NoSuchAlgorithmException, KeyManagementException { public static String httpClientGet(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws NoSuchAlgorithmException, KeyManagementException {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -724,18 +818,24 @@ public class HttpClientHelper {
// 构建请求参数 // 构建请求参数
StringBuffer sbParams = new StringBuffer(); StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) { if (params != null && params.size() > 0) {
for (Entry<String, Object> entry : params.entrySet()) { if(1==params.size()&&params.containsKey("_forTextJson")){
sbParams.append(entry.getKey()); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用httpClientGet请求(必须为key-value)...");
sbParams.append("="); return "协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用httpClientGet请求(必须为key-value)...";
try { }else{
sbParams.append(URLEncoder.encode(String.valueOf(entry.getValue()), charset)); for (Entry<String, Object> entry : params.entrySet()) {
} catch (UnsupportedEncodingException e) { sbParams.append(entry.getKey());
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e); sbParams.append("=");
throw new RuntimeException(e); try {
sbParams.append(URLEncoder.encode(String.valueOf(entry.getValue()), charset));
} catch (UnsupportedEncodingException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e);
}
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置HTTPClientGet参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+"");
} }
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置HTTPClientGet参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+"");
} }
} }
if (sbParams != null && sbParams.length() > 0) { if (sbParams != null && sbParams.length() > 0) {
urlParam = urlParam + "?" + sbParams.substring(0, sbParams.length() - 1); urlParam = urlParam + "?" + sbParams.substring(0, sbParams.length() - 1);
@ -785,7 +885,12 @@ public class HttpClientHelper {
} }
/** /**
* @Description:使用socket发送post请求 * 使用socket发送post请求
* @param urlParam
* @param params
* @param charset
* @param headmsg
* @return
*/ */
public static String sendSocketPost(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg) { public static String sendSocketPost(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg) {
String result = ""; String result = "";
@ -793,12 +898,17 @@ public class HttpClientHelper {
// 构建请求参数 // 构建请求参数
StringBuffer sbParams = new StringBuffer(); StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) { if (params != null && params.size() > 0) {
for (Entry<String, Object> entry : params.entrySet()) { if(1==params.size()&&params.containsKey("_forTextJson")){
sbParams.append(entry.getKey()); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendSocketPost请求(必须为key-value)...");
sbParams.append("="); return "协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendSocketPost请求(必须为key-value)...";
sbParams.append(entry.getValue()); }else{
sbParams.append("&"); for (Entry<String, Object> entry : params.entrySet()) {
luckyclient.publicclass.LogUtil.APP.info("设置SocketPost参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+""); sbParams.append(entry.getKey());
sbParams.append("=");
sbParams.append(entry.getValue());
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置SocketPost参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+"");
}
} }
} }
Socket socket = null; Socket socket = null;
@ -903,7 +1013,12 @@ public class HttpClientHelper {
} }
/** /**
* @Description:使用socket发送get请求 * 使用socket发送get请求
* @param urlParam
* @param params
* @param charset
* @param headmsg
* @return
*/ */
public static String sendSocketGet(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg) { public static String sendSocketGet(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg) {
String result = ""; String result = "";
@ -911,13 +1026,19 @@ public class HttpClientHelper {
// 构建请求参数 // 构建请求参数
StringBuffer sbParams = new StringBuffer(); StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) { if (params != null && params.size() > 0) {
for (Entry<String, Object> entry : params.entrySet()) { if(1==params.size()&&params.containsKey("_forTextJson")){
sbParams.append(entry.getKey()); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendSocketGet请求(必须为key-value)...");
sbParams.append("="); return "协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendSocketGet请求(必须为key-value)...";
sbParams.append(entry.getValue()); }else{
sbParams.append("&"); for (Entry<String, Object> entry : params.entrySet()) {
luckyclient.publicclass.LogUtil.APP.info("设置SocketPost参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+""); sbParams.append(entry.getKey());
sbParams.append("=");
sbParams.append(entry.getValue());
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置SocketPost参数信息...key:【"+entry.getKey()+"】 value:【"+entry.getValue()+"");
}
} }
} }
Socket socket = null; Socket socket = null;
OutputStreamWriter osw = null; OutputStreamWriter osw = null;
@ -1021,7 +1142,12 @@ public class HttpClientHelper {
} }
/** /**
* @Description:读取一行数据contentLe内容长度为0时读取响应头信息不为0时读正文 * 读取一行数据contentLe内容长度为0时读取响应头信息不为0时读正文
* @param is
* @param contentLength
* @param charset
* @return
* @throws IOException
*/ */
private static String readLine(InputStream is, int contentLength, String charset) throws IOException { private static String readLine(InputStream is, int contentLength, String charset) throws IOException {
List<Byte> lineByte = new ArrayList<Byte>(); List<Byte> lineByte = new ArrayList<Byte>();
@ -1049,8 +1175,14 @@ public class HttpClientHelper {
return new String(resutlBytes, charset); return new String(resutlBytes, charset);
} }
/** /**
* @Description:使用HttpURLConnection发送delete请求 * 使用HttpURLConnection发送delete请求
* @param urlParam
* @param params
* @param charset
* @param timeout
* @param headmsg
* @return
*/ */
public static String sendHttpURLDel(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) { public static String sendHttpURLDel(String urlParam, Map<String, Object> params, String charset, int timeout,Map<String, String> headmsg) {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -1059,12 +1191,17 @@ public class HttpClientHelper {
luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+""); luckyclient.publicclass.LogUtil.APP.info("设置HTTP请求地址:【"+urlParam+"");
StringBuffer sbParams = new StringBuffer(); StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) { if (params != null && params.size() > 0) {
for (Entry<String, Object> e : params.entrySet()) { if(1==params.size()&&params.containsKey("_forTextJson")){
sbParams.append(e.getKey()); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendHttpURLDel请求(必须为key-value)...");
sbParams.append("="); return "协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用sendHttpURLDel请求(必须为key-value)...";
sbParams.append(e.getValue()); }else{
sbParams.append("&"); for (Entry<String, Object> e : params.entrySet()) {
luckyclient.publicclass.LogUtil.APP.info("设置HttpURLDel参数信息...key:【"+e.getKey()+"】 value:【"+e.getValue()+""); sbParams.append(e.getKey());
sbParams.append("=");
sbParams.append(e.getValue());
sbParams.append("&");
luckyclient.publicclass.LogUtil.APP.info("设置HttpURLDel参数信息...key:【"+e.getKey()+"】 value:【"+e.getValue()+"");
}
} }
} }
HttpURLConnection con = null; HttpURLConnection con = null;
@ -1150,10 +1287,16 @@ public class HttpClientHelper {
} }
/** /**
* @throws NoSuchAlgorithmException * 使用HttpClient发送put请求 参数JSON格式
* @throws KeyManagementException * @param urlParam
* @Description:使用HttpClient发送put请求 参数JSON格式 * @param params
* @param charset
* @param headmsg
* @param cerpath
* @return
* @throws KeyManagementException
* @throws NoSuchAlgorithmException
*/ */
public static String httpClientPutJson(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws KeyManagementException, NoSuchAlgorithmException { public static String httpClientPutJson(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws KeyManagementException, NoSuchAlgorithmException {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -1180,11 +1323,18 @@ public class HttpClientHelper {
BufferedReader br = null; BufferedReader br = null;
try { try {
if(params.size()>0){ if(params.size()>0){
if(1==params.size()&&params.containsKey("_forTextJson")){
luckyclient.publicclass.LogUtil.APP.info("参数类型TEXT,设置HTTPClientPutJson参数信息...【"+params.get("_forTextJson").toString()+"");
StringEntity entity = new StringEntity(params.get("_forTextJson").toString(),charset);
httpput.setEntity(entity);
}else{
String jsonString = JSON.toJSONString(params); String jsonString = JSON.toJSONString(params);
luckyclient.publicclass.LogUtil.APP.info("设置HTTPClientPutJson参数信息...【"+jsonString+""); luckyclient.publicclass.LogUtil.APP.info("参数类型FORM,设置HTTPClientPutJson参数信息...【"+jsonString+"");
StringEntity entity = new StringEntity(jsonString,charset); StringEntity entity = new StringEntity(jsonString,charset);
httpput.setEntity(entity); httpput.setEntity(entity);
} }
}
CloseableHttpResponse response = httpclient.execute(httpput); CloseableHttpResponse response = httpclient.execute(httpput);
@ -1219,9 +1369,15 @@ public class HttpClientHelper {
} }
/** /**
* @throws NoSuchAlgorithmException * 使用HttpClient发送put请求
* @throws KeyManagementException * @param urlParam
* @Description:使用HttpClient发送put请求 * @param params
* @param charset
* @param headmsg
* @param cerpath
* @return
* @throws KeyManagementException
* @throws NoSuchAlgorithmException
*/ */
public static String httpClientPut(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws KeyManagementException, NoSuchAlgorithmException { public static String httpClientPut(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg,String cerpath) throws KeyManagementException, NoSuchAlgorithmException {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
@ -1247,13 +1403,19 @@ public class HttpClientHelper {
BufferedReader br = null; BufferedReader br = null;
try { try {
if(params.size()>0){ if(params.size()>0){
//拼接参数 if(1==params.size()&&params.containsKey("_forTextJson")){
List <NameValuePair> nvps = new ArrayList <NameValuePair>(); luckyclient.publicclass.LogUtil.APP.error("协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用httpClientPut请求(必须为key-value)...");
for (Map.Entry<String, Object> m :params.entrySet()) { return "协议模板是纯文本模式(仅限httpClientPostJson以及httpClientPutJson请求)无法使用httpClientPut请求(必须为key-value)...";
nvps.add(new BasicNameValuePair(m.getKey(), m.getValue().toString())); }else{
luckyclient.publicclass.LogUtil.APP.info("开始设置HTTPClientPut参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+""); //拼接参数
} List <NameValuePair> nvps = new ArrayList <NameValuePair>();
httpput.setEntity(new UrlEncodedFormEntity(nvps,charset)); for (Map.Entry<String, Object> m :params.entrySet()) {
nvps.add(new BasicNameValuePair(m.getKey(), m.getValue().toString()));
luckyclient.publicclass.LogUtil.APP.info("开始设置HTTPClientPut参数信息...key:【"+m.getKey()+"】 value:【"+m.getValue()+"");
}
httpput.setEntity(new UrlEncodedFormEntity(nvps,charset));
}
} }
CloseableHttpResponse response = httpclient.execute(httpput); CloseableHttpResponse response = httpclient.execute(httpput);
@ -1289,19 +1451,11 @@ public class HttpClientHelper {
} }
/** /**
* 设置信任自签名证书 * 设置信任自签名证书
* @param keyStorePath
* * @param keyStorepass
* @param keyStorePath 密钥库路径
* @param keyStorepass 密钥库密码
* @return * @return
*/ */
private static SSLContext sslContextKeyStore(String keyStorePath, String keyStorepass) { private static SSLContext sslContextKeyStore(String keyStorePath, String keyStorepass) {
SSLContext sslContext = null; SSLContext sslContext = null;
FileInputStream instream = null; FileInputStream instream = null;
@ -1377,6 +1531,6 @@ public class HttpClientHelper {
} }
public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException { public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException {
} }
} }

View File

@ -70,7 +70,7 @@ public class HttpImpl {
File file =new File(System.getProperty("user.dir")+loadpath); File file =new File(System.getProperty("user.dir")+loadpath);
if (!file .isDirectory()) if (!file .isDirectory())
{ {
luckyclient.publicclass.LogUtil.APP.info("客户端测试驱动桩路径不存在,请检查【"+file.getPath()+""); luckyclient.publicclass.LogUtil.APP.error("客户端测试驱动桩路径不存在,请检查【"+file.getPath()+"");
return "客户端测试驱动桩路径不存在,请检查【"+file.getPath()+""; return "客户端测试驱动桩路径不存在,请检查【"+file.getPath()+"";
} }
Runtime run = Runtime.getRuntime(); Runtime run = Runtime.getRuntime();
@ -85,6 +85,7 @@ public class HttpImpl {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
luckyclient.publicclass.LogUtil.APP.error("启动任务模式测试程序异常!!!",e);
return "启动任务模式测试程序异常!!!"; return "启动任务模式测试程序异常!!!";
} }
return "启动任务模式测试程序正常"; return "启动任务模式测试程序正常";
@ -121,7 +122,7 @@ public class HttpImpl {
File file =new File(System.getProperty("user.dir")+loadpath); File file =new File(System.getProperty("user.dir")+loadpath);
if (!file .isDirectory()) if (!file .isDirectory())
{ {
luckyclient.publicclass.LogUtil.APP.info("客户端测试驱动桩路径不存在,请检查【"+file.getPath()+""); luckyclient.publicclass.LogUtil.APP.error("客户端测试驱动桩路径不存在,请检查【"+file.getPath()+"");
return "客户端测试驱动桩路径不存在,请检查【"+file.getPath()+""; return "客户端测试驱动桩路径不存在,请检查【"+file.getPath()+"";
} }
Runtime run = Runtime.getRuntime(); Runtime run = Runtime.getRuntime();
@ -138,6 +139,7 @@ public class HttpImpl {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
luckyclient.publicclass.LogUtil.APP.error("启动单用例模式测试程序异常!!!",e);
return "启动单用例模式测试程序异常!!!"; return "启动单用例模式测试程序异常!!!";
} }
return "启动单用例模式测试程序正常"; return "启动单用例模式测试程序正常";
@ -172,7 +174,7 @@ public class HttpImpl {
File file =new File(System.getProperty("user.dir")+loadpath); File file =new File(System.getProperty("user.dir")+loadpath);
if (!file .isDirectory()) if (!file .isDirectory())
{ {
luckyclient.publicclass.LogUtil.APP.info("客户端测试驱动桩路径不存在,请检查【"+file.getPath()+""); luckyclient.publicclass.LogUtil.APP.error("客户端测试驱动桩路径不存在,请检查【"+file.getPath()+"");
return "客户端测试驱动桩路径不存在,请检查【"+file.getPath()+""; return "客户端测试驱动桩路径不存在,请检查【"+file.getPath()+"";
} }
Runtime run = Runtime.getRuntime(); Runtime run = Runtime.getRuntime();
@ -188,6 +190,7 @@ public class HttpImpl {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
luckyclient.publicclass.LogUtil.APP.error("启动批量用例模式测试程序异常!!!",e);
return "启动批量用例模式测试程序异常!!!"; return "启动批量用例模式测试程序异常!!!";
} }
return "启动批量用例模式测试程序正常"; return "启动批量用例模式测试程序正常";
@ -220,7 +223,7 @@ public class HttpImpl {
File file =new File(System.getProperty("user.dir")+loadpath); File file =new File(System.getProperty("user.dir")+loadpath);
if (!file .isDirectory()) if (!file .isDirectory())
{ {
luckyclient.publicclass.LogUtil.APP.info("客户端测试驱动桩路径不存在,请检查【"+file.getPath()+""); luckyclient.publicclass.LogUtil.APP.error("客户端测试驱动桩路径不存在,请检查【"+file.getPath()+"");
return "客户端测试驱动桩路径不存在,请检查【"+file.getPath()+""; return "客户端测试驱动桩路径不存在,请检查【"+file.getPath()+"";
} }
Runtime run = Runtime.getRuntime(); Runtime run = Runtime.getRuntime();
@ -236,6 +239,7 @@ public class HttpImpl {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
luckyclient.publicclass.LogUtil.APP.error("启动Web调试模式测试程序异常",e);
return "启动Web调试模式测试程序异常"; return "启动Web调试模式测试程序异常";
} }
return "启动Web调试模式测试程序正常"; return "启动Web调试模式测试程序正常";
@ -263,6 +267,7 @@ public class HttpImpl {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
luckyclient.publicclass.LogUtil.APP.error("读取日志路径错误,请检查客户端日志路径是否存在!downLoadPath: "+downLoadPath,e);
return "读取日志路径错误,请检查客户端日志路径是否存在!downLoadPath: "+downLoadPath; return "读取日志路径错误,请检查客户端日志路径是否存在!downLoadPath: "+downLoadPath;
} }
BufferedReader bos = new BufferedReader(isr); BufferedReader bos = new BufferedReader(isr);
@ -277,6 +282,7 @@ public class HttpImpl {
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
luckyclient.publicclass.LogUtil.APP.error("客户端转BufferedReader失败请检查原因",e);
return "客户端转BufferedReader失败请检查原因"; return "客户端转BufferedReader失败请检查原因";
} }
return sb.toString(); return sb.toString();
@ -304,6 +310,7 @@ public class HttpImpl {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
luckyclient.publicclass.LogUtil.APP.error("此文件不存在,请检查:"+downLoadPath,e);
return b; return b;
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
@ -318,9 +325,11 @@ public class HttpImpl {
if (!jarfile.isEmpty()){ if (!jarfile.isEmpty()){
if (!FilenameUtils.getExtension(jarfile.getOriginalFilename()) if (!FilenameUtils.getExtension(jarfile.getOriginalFilename())
.equalsIgnoreCase("jar")) { .equalsIgnoreCase("jar")) {
luckyclient.publicclass.LogUtil.APP.error("文件格式后续不是.jar上传失败");
return "文件格式后续不是.jar上传失败"; return "文件格式后续不是.jar上传失败";
} }
}else{ }else{
luckyclient.publicclass.LogUtil.APP.error("上传文件为空,请检查!");
return "上传文件为空,请检查!"; return "上传文件为空,请检查!";
} }
@ -329,7 +338,7 @@ public class HttpImpl {
String path = System.getProperty("user.dir")+loadpath; String path = System.getProperty("user.dir")+loadpath;
if (!new File(path) .isDirectory()) if (!new File(path) .isDirectory())
{ {
luckyclient.publicclass.LogUtil.APP.info("客户端测试驱动桩路径不存在,请检查【"+path+""); luckyclient.publicclass.LogUtil.APP.error("客户端测试驱动桩路径不存在,请检查【"+path+"");
return "客户端测试驱动桩路径不存在,请检查【"+path+""; return "客户端测试驱动桩路径不存在,请检查【"+path+"";
} }
String pathName = path +File.separator+ name; String pathName = path +File.separator+ name;
@ -350,10 +359,12 @@ public class HttpImpl {
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
luckyclient.publicclass.LogUtil.APP.error("客户端未找到正确路径或文件,上传失败!文件路径名称:"+pathName,e);
return "客户端未找到正确路径或文件,上传失败!文件路径名称:"+pathName; return "客户端未找到正确路径或文件,上传失败!文件路径名称:"+pathName;
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
luckyclient.publicclass.LogUtil.APP.error("客户端IOExceptiona或是未找到驱动路径文件路径名称"+pathName,e);
return "客户端IOExceptiona或是未找到驱动路径文件路径名称"+pathName; return "客户端IOExceptiona或是未找到驱动路径文件路径名称"+pathName;
} }
} }
@ -384,14 +395,14 @@ public class HttpImpl {
websocket.connect(new InetSocketAddress(webip, webport)); websocket.connect(new InetSocketAddress(webip, webport));
luckyclient.publicclass.LogUtil.APP.info("客户端访问Web端配置"+webip+":"+webport+" 检测通过......"); luckyclient.publicclass.LogUtil.APP.info("客户端访问Web端配置"+webip+":"+webport+" 检测通过......");
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.info("客户端配置检测异常,请确认您项目根目录下的客户端配置文件(sys_config.properties)是否已经正确配置。",e); luckyclient.publicclass.LogUtil.APP.error("客户端配置检测异常,请确认您项目根目录下的客户端配置文件(sys_config.properties)是否已经正确配置。",e);
return false; return false;
} finally { } finally {
try { try {
dbsocket.close(); dbsocket.close();
websocket.close(); websocket.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.info("关闭Socket链接异常......",e); luckyclient.publicclass.LogUtil.APP.error("关闭Socket链接异常......",e);
} }
} }
return true; return true;