1:优化用例无步骤执行任务时的逻辑

2:增加HTTPPUTJSON方法
3:增加支持用例参数中修改传参协议模板中的头域
This commit is contained in:
seagull 2017-12-13 15:43:59 +08:00
parent 0ed669d9d8
commit d7f02dd9b9
7 changed files with 199 additions and 60 deletions

View File

@ -11,7 +11,6 @@ import luckyclient.planapi.entity.ProjectCase;
import luckyclient.planapi.entity.ProjectCasesteps; import luckyclient.planapi.entity.ProjectCasesteps;
import luckyclient.planapi.entity.PublicCaseParams; import luckyclient.planapi.entity.PublicCaseParams;
import luckyclient.publicclass.ChangString; import luckyclient.publicclass.ChangString;
import luckyclient.publicclass.DBOperation;
import luckyclient.publicclass.InvokeMethod; import luckyclient.publicclass.InvokeMethod;
/** /**
* ================================================================= * =================================================================
@ -31,7 +30,7 @@ public class ApiTestCaseDebug{
* @param 项目名 * @param 项目名
* @param 用例编号 * @param 用例编号
* @param 用例版本号 * @param 用例版本号
* 用于在testlink上配置好用例参数后做单条用例调试 * 用于在本地做单条用例调试
*/ */
public static void oneCaseDebug(String projectname,String testCaseExternalId){ public static void oneCaseDebug(String projectname,String testCaseExternalId){
Map<String,String> variable = new HashMap<String,String>(0); Map<String,String> variable = new HashMap<String,String>(0);
@ -49,6 +48,11 @@ public class ApiTestCaseDebug{
variable.put(pcp.getParamsname(), pcp.getParamsvalue()); variable.put(pcp.getParamsname(), pcp.getParamsvalue());
} }
List<ProjectCasesteps> steps=GetServerAPI.getStepsbycaseid(testcaseob.getId()); List<ProjectCasesteps> steps=GetServerAPI.getStepsbycaseid(testcaseob.getId());
if(steps.size()==0){
setresult=2;
luckyclient.publicclass.LogUtil.APP.error("用例中未找到步骤,请检查!");
testnote="用例中未找到步骤,请检查!";
}
//进入循环解析用例所有步骤 //进入循环解析用例所有步骤
for(int i=0;i<steps.size();i++){ for(int i=0;i<steps.size();i++){
Map<String,String> casescript = InterfaceAnalyticCase.analyticCaseStep(testcaseob, steps.get(i),"888888",null); Map<String,String> casescript = InterfaceAnalyticCase.analyticCaseStep(testcaseob, steps.get(i),"888888",null);
@ -155,7 +159,7 @@ public class ApiTestCaseDebug{
* @param 项目名 * @param 项目名
* @param 用例编号 * @param 用例编号
* @param 用例版本号 * @param 用例版本号
* 用于在testlink上配置好用例参数后做多条用例串行调试 * 用于在本地做多条用例串行调试
*/ */
public static void moreCaseDebug(String projectname,Map<String,Integer> addtestcase){ public static void moreCaseDebug(String projectname,Map<String,Integer> addtestcase){
System.out.println(addtestcase.size()); System.out.println(addtestcase.size());

View File

@ -56,6 +56,12 @@ public class TestCaseExecution {
variable.put(pcp.getParamsname(), pcp.getParamsvalue()); variable.put(pcp.getParamsname(), pcp.getParamsvalue());
} }
List<ProjectCasesteps> steps = GetServerAPI.getStepsbycaseid(testcaseob.getId()); List<ProjectCasesteps> steps = GetServerAPI.getStepsbycaseid(testcaseob.getId());
if(steps.size()==0){
setresult=2;
luckyclient.publicclass.LogUtil.APP.error("用例中未找到步骤,请检查!");
LogOperation.updateCaseLogDetail(testCaseExternalId, taskid,"用例中未找到步骤,请检查!", "error", "1");
testnote="用例中未找到步骤,请检查!";
}
// 进入循环解析用例所有步骤 // 进入循环解析用例所有步骤
for (int i = 0; i < steps.size(); i++) { for (int i = 0; i < steps.size(); i++) {
Map<String, String> casescript = InterfaceAnalyticCase.analyticCaseStep(testcaseob, steps.get(i), taskid,caselog); Map<String, String> casescript = InterfaceAnalyticCase.analyticCaseStep(testcaseob, steps.get(i), taskid,caselog);
@ -167,21 +173,15 @@ public class TestCaseExecution {
} }
variable.clear(); // 清空传参MAP variable.clear(); // 清空传参MAP
// 如果调用方法过程中未出错进入设置测试结果流程 // 如果调用方法过程中未出错进入设置测试结果流程
if (testnote.indexOf("CallCase调用出错") <= -1 && testnote.indexOf("解析出错啦!") <= -1) { if (testnote.indexOf("CallCase调用出错") <0 && testnote.indexOf("解析出错啦!") <0) {
luckyclient.publicclass.LogUtil.APP.info("用例 " + testCaseExternalId + "解析成功,并成功调用用例中方法,请继续查看执行结果!"); luckyclient.publicclass.LogUtil.APP.info("用例 " + testCaseExternalId + "解析成功,并成功调用用例中方法,请继续查看执行结果!");
LogOperation.updateCaseLogDetail(testCaseExternalId, taskid, "解析成功,并成功调用用例中方法,请继续查看执行结果!", "info", LogOperation.updateCaseLogDetail(testCaseExternalId, taskid, "解析成功,并成功调用用例中方法,请继续查看执行结果!", "info",
"SETCASERESULT..."); "SETCASERESULT...");
// TCResult =
// TestCaseApi.setTCResult(projectname,testCaseExternalId, testnote,
// version,setresult);
caselog.updateCaseDetail(taskid, testCaseExternalId, setresult); caselog.updateCaseDetail(taskid, testCaseExternalId, setresult);
} else { } else {
setresult = 1; setresult = 1;
luckyclient.publicclass.LogUtil.APP.error("用例 " + testCaseExternalId + "解析或是调用步骤中的方法出错!"); luckyclient.publicclass.LogUtil.APP.error("用例 " + testCaseExternalId + "解析或是调用步骤中的方法出错!");
LogOperation.updateCaseLogDetail(testCaseExternalId, taskid, "解析或是调用步骤中的方法出错!", "error", "SETCASERESULT..."); LogOperation.updateCaseLogDetail(testCaseExternalId, taskid, "解析或是调用步骤中的方法出错!", "error", "SETCASERESULT...");
// TCResult =
// TestCaseApi.setTCResult(projectname,testCaseExternalId, testnote,
// version,2);
caselog.updateCaseDetail(taskid, testCaseExternalId, 2); caselog.updateCaseDetail(taskid, testCaseExternalId, 2);
} }
if (0 == setresult) { if (0 == setresult) {
@ -216,6 +216,12 @@ public class TestCaseExecution {
variable.put(pcp.getParamsname(), pcp.getParamsvalue()); variable.put(pcp.getParamsname(), pcp.getParamsvalue());
} }
List<ProjectCasesteps> steps = GetServerAPI.getStepsbycaseid(testcaseob.getId()); List<ProjectCasesteps> steps = GetServerAPI.getStepsbycaseid(testcaseob.getId());
if(steps.size()==0){
setresult=2;
luckyclient.publicclass.LogUtil.APP.error("用例中未找到步骤,请检查!");
caselog.caseLogDetail(taskid, testcaseob.getSign(), "用例中未找到步骤,请检查!", "error", "1", "");
testnote="用例中未找到步骤,请检查!";
}
// 进入循环解析用例所有步骤 // 进入循环解析用例所有步骤
for (int i = 0; i < steps.size(); i++) { for (int i = 0; i < steps.size(); i++) {
Map<String, String> casescript = InterfaceAnalyticCase.analyticCaseStep(testcaseob, steps.get(i), taskid,caselog); Map<String, String> casescript = InterfaceAnalyticCase.analyticCaseStep(testcaseob, steps.get(i), taskid,caselog);

View File

@ -55,6 +55,7 @@ public class TestControl {
TestBuildApi.getBuild(projectname); TestBuildApi.getBuild(projectname);
TestCase[] testCases = TestCaseApi.getplantestcase(projectname, "NULL", testplan); TestCase[] testCases = TestCaseApi.getplantestcase(projectname, "NULL", testplan);
String taskid = "888888"; String taskid = "888888";
for (TestCase testcase : testCases) { for (TestCase testcase : testCases) {
if (testcase.getSteps().size() == 0) { if (testcase.getSteps().size() == 0) {
continue; continue;
@ -97,14 +98,19 @@ public class TestControl {
} }
String taskid = "888888"; String taskid = "888888";
// 初始化写用例结果以及日志模块
LogOperation caselog = new LogOperation();
for (ProjectCase testcase : testCases) { for (ProjectCase testcase : testCases) {
List<ProjectCasesteps> steps = GetServerAPI.getStepsbycaseid(testcase.getId()); List<ProjectCasesteps> steps = GetServerAPI.getStepsbycaseid(testcase.getId());
if (steps.size() == 0) { if (steps.size() == 0) {
caselog.addCaseDetail(taskid, testcase.getSign(), "1", testcase.getName(), 2);
luckyclient.publicclass.LogUtil.APP.error("用例【" + testcase.getSign() + "】没有找到步骤,直接跳过,请检查!");
caselog.caseLogDetail(taskid, testcase.getSign(),"在用例中没有找到步骤,请检查","error", "1", "");
continue; continue;
} }
Debugcount++; // 多线程计数++用于检测线程是否全部执行完 Debugcount++; // 多线程计数++用于检测线程是否全部执行完
threadExecute threadExecute
.execute(new ThreadForExecuteCase(testcase, steps,taskid,pcplist)); .execute(new ThreadForExecuteCase(testcase, steps,taskid,pcplist,caselog));
} }
// 多线程计数用于检测线程是否全部执行完 // 多线程计数用于检测线程是否全部执行完
int i = 0; int i = 0;
@ -134,6 +140,8 @@ public class TestControl {
String projectname=task.getTestJob().getPlanproj(); String projectname=task.getTestJob().getPlanproj();
int timeout = task.getTestJob().getTimeout(); int timeout = task.getTestJob().getTimeout();
List<PublicCaseParams> pcplist=GetServerAPI.cgetParamsByProjectid(task.getTestJob().getProjectid().toString()); List<PublicCaseParams> pcplist=GetServerAPI.cgetParamsByProjectid(task.getTestJob().getProjectid().toString());
// 初始化写用例结果以及日志模块
LogOperation caselog = new LogOperation();
// 判断是否要自动重启TOMCAT // 判断是否要自动重启TOMCAT
if (restartstatus.indexOf("Status:true") > -1) { if (restartstatus.indexOf("Status:true") > -1) {
// 判断是否构建是否成功 // 判断是否构建是否成功
@ -174,11 +182,14 @@ public class TestControl {
ProjectCase projectcase =cases.get(j); ProjectCase projectcase =cases.get(j);
List<ProjectCasesteps> steps=GetServerAPI.getStepsbycaseid(projectcase.getId()); List<ProjectCasesteps> steps=GetServerAPI.getStepsbycaseid(projectcase.getId());
if (steps.size()== 0) { if (steps.size()== 0) {
caselog.addCaseDetail(taskid, projectcase.getSign(), "1", projectcase.getName(), 2);
luckyclient.publicclass.LogUtil.APP.error("用例【" + projectcase.getSign() + "】没有找到步骤,直接跳过,请检查!");
caselog.caseLogDetail(taskid, projectcase.getSign(),"在用例中没有找到步骤,请检查","error", "1", "");
continue; continue;
} }
Debugcount++; // 多线程计数++用于检测线程是否全部执行完 Debugcount++; // 多线程计数++用于检测线程是否全部执行完
threadExecute.execute( threadExecute.execute(
new ThreadForExecuteCase(projectcase, steps,taskid,pcplist)); new ThreadForExecuteCase(projectcase, steps,taskid,pcplist,caselog));
} }
// 多线程计数用于检测线程是否全部执行完 // 多线程计数用于检测线程是否全部执行完
int i = 0; int i = 0;

View File

@ -31,14 +31,16 @@ public class ThreadForExecuteCase extends Thread {
private String taskid; private String taskid;
private List<ProjectCasesteps> steps; private List<ProjectCasesteps> steps;
private List<PublicCaseParams> pcplist; private List<PublicCaseParams> pcplist;
private LogOperation caselog;
public ThreadForExecuteCase(ProjectCase projectcase, List<ProjectCasesteps> steps, String taskid, public ThreadForExecuteCase(ProjectCase projectcase, List<ProjectCasesteps> steps, String taskid,
List<PublicCaseParams> pcplist) { List<PublicCaseParams> pcplist,LogOperation caselog) {
this.caseid = projectcase.getSign(); this.caseid = projectcase.getSign();
this.testcaseob = projectcase; this.testcaseob = projectcase;
this.taskid = taskid; this.taskid = taskid;
this.steps = steps; this.steps = steps;
this.pcplist = pcplist; this.pcplist = pcplist;
this.caselog = caselog;
} }
@Override @Override
@ -48,8 +50,6 @@ public class ThreadForExecuteCase extends Thread {
for (PublicCaseParams pcp : pcplist) { for (PublicCaseParams pcp : pcplist) {
variable.put(pcp.getParamsname(), pcp.getParamsvalue()); variable.put(pcp.getParamsname(), pcp.getParamsvalue());
} }
// 初始化写用例结果以及日志模块
LogOperation caselog = new LogOperation();
String functionname = null; String functionname = null;
String packagename = null; String packagename = null;
String expectedresults = null; String expectedresults = null;
@ -185,7 +185,7 @@ public class ThreadForExecuteCase extends Thread {
// 如果调用方法过程中未出错进入设置测试结果流程 // 如果调用方法过程中未出错进入设置测试结果流程
try { try {
// 成功跟失败的用例走此流程 // 成功跟失败的用例走此流程
if (testnote.indexOf("CallCase调用出错") <= -1 && testnote.indexOf("解析出错啦!") <= -1) { if (testnote.indexOf("CallCase调用出错") <0 && testnote.indexOf("解析出错啦!") <0) {
caselog.updateCaseDetail(taskid, caseid, setresult); caselog.updateCaseDetail(taskid, caseid, setresult);
} else { } else {
// 解析用例或是调用方法出错全部把用例置为锁定 // 解析用例或是调用方法出错全部把用例置为锁定

View File

@ -86,15 +86,36 @@ public class InvokeMethod {
jsonarr.set(i, tempobj); jsonarr.set(i, tempobj);
} }
} }
//´¦ÀíÍ·Óò
Map<String, String> headmsg = new HashMap<String, String>(0);
if(null!=ppt.getHeadmsg()&&!ppt.getHeadmsg().equals("")&&ppt.getHeadmsg().indexOf("=")>0){
String[] temp=ppt.getHeadmsg().split(";",-1);
for(int i=0;i<temp.length;i++){
if(null!=temp[i]&&!temp[i].equals("")&&temp[i].indexOf("=")>0){
String key=temp[i].substring(0, temp[i].indexOf("="));
String value=temp[i].substring(temp[i].indexOf("=")+1);
headmsg.put(key, value);
}
}
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<ProjectTemplateParams> paramslist = JSONArray.toList(jsonarr, new ProjectTemplateParams(), List<ProjectTemplateParams> paramslist = JSONArray.toList(jsonarr, new ProjectTemplateParams(),
new JsonConfig()); new JsonConfig());
//处理更换参数 //处理更换参数
if(null!=getParameterValues){ if(null!=getParameterValues){
String booleanheadmsg="headmsg(";
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("#")); String key = paramob.substring(0, paramob.indexOf("#"));
String value = paramob.substring(paramob.indexOf("#") + 1); String value = paramob.substring(paramob.indexOf("#") + 1);
if(key.indexOf(booleanheadmsg)>-1&&key.indexOf(msgend)>-1){
String head=key.substring(key.indexOf(booleanheadmsg)+8, key.lastIndexOf(msgend));
headmsg.put(head, value);
continue;
}
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(ptp.getParamname().equals(key)){
@ -123,18 +144,6 @@ public class InvokeMethod {
params.put(ptp.getParamname().replaceAll("&quot;", "\""), ptp.getParam().replaceAll("&quot;", "\"")); params.put(ptp.getParamname().replaceAll("&quot;", "\""), ptp.getParam().replaceAll("&quot;", "\""));
} }
} }
//´¦ÀíÍ·Óò
Map<String, String> headmsg = new HashMap<String, String>(0);
if(null!=ppt.getHeadmsg()&&!ppt.getHeadmsg().equals("")&&ppt.getHeadmsg().indexOf("=")>0){
String[] temp=ppt.getHeadmsg().split(";",-1);
for(int i=0;i<temp.length;i++){
if(null!=temp[i]&&!temp[i].equals("")&&temp[i].indexOf("=")>0){
String key=temp[i].substring(0, temp[i].indexOf("="));
String value=temp[i].substring(temp[i].indexOf("=")+1);
headmsg.put(key, value);
}
}
}
for (ProjectTemplateParams ptp : paramslist) { for (ProjectTemplateParams ptp : paramslist) {
if(ptp.getParam().indexOf("***[")>-1&&"***[".equals(ptp.getParam().substring(0, 4))){ if(ptp.getParam().indexOf("***[")>-1&&"***[".equals(ptp.getParam().substring(0, 4))){
@ -168,7 +177,10 @@ public class InvokeMethod {
} else if (functionname.toLowerCase().equals("httpurldelete")) { } else if (functionname.toLowerCase().equals("httpurldelete")) {
result = HttpClientHelper.sendHttpURLDel(packagename, params, result = HttpClientHelper.sendHttpURLDel(packagename, params,
ppt.getContentencoding().toLowerCase(),ppt.getConnecttimeout(),headmsg); ppt.getContentencoding().toLowerCase(),ppt.getConnecttimeout(),headmsg);
} else if (functionname.toLowerCase().equals("httpclientput")) { } else if (functionname.toLowerCase().equals("httpclientputjson")) {
result = HttpClientHelper.httpClientPutJson(packagename, params,
ppt.getContentencoding().toLowerCase(),headmsg);
} else if (functionname.toLowerCase().equals("httpclientput")) {
result = HttpClientHelper.httpClientPut(packagename, params, result = HttpClientHelper.httpClientPut(packagename, params,
ppt.getContentencoding().toLowerCase(),headmsg); ppt.getContentencoding().toLowerCase(),headmsg);
} else if (functionname.toLowerCase().equals("httpclientget")) { } else if (functionname.toLowerCase().equals("httpclientget")) {
@ -204,15 +216,36 @@ public class InvokeMethod {
jsonarr.set(i, tempobj); jsonarr.set(i, tempobj);
} }
} }
//´¦ÀíÍ·Óò
Map<String, String> headmsg = new HashMap<String, String>(0);
if(null!=ppt.getHeadmsg()&&!ppt.getHeadmsg().equals("")&&ppt.getHeadmsg().indexOf("=")>0){
String[] temp=ppt.getHeadmsg().split(";",-1);
for(int i=0;i<temp.length;i++){
if(null!=temp[i]&&!temp[i].equals("")&&temp[i].indexOf("=")>0){
String key=temp[i].substring(0, temp[i].indexOf("="));
String value=temp[i].substring(temp[i].indexOf("=")+1);
headmsg.put(key, value);
}
}
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<ProjectTemplateParams> paramslist = JSONArray.toList(jsonarr, new ProjectTemplateParams(), List<ProjectTemplateParams> paramslist = JSONArray.toList(jsonarr, new ProjectTemplateParams(),
new JsonConfig()); new JsonConfig());
//处理更换参数 //处理更换参数
if(null!=getParameterValues){ if(null!=getParameterValues){
String booleanheadmsg="headmsg(";
String msgend=")";
for (Object obp : getParameterValues) { for (Object obp : getParameterValues) {
String paramob = obp.toString(); String paramob = obp.toString();
String key = paramob.substring(0, action.indexOf("#")); String key = paramob.substring(0, paramob.indexOf("#"));
String value = paramob.substring(action.indexOf("#") + 1); String value = paramob.substring(paramob.indexOf("#") + 1);
if(key.indexOf(booleanheadmsg)>-1&&key.indexOf(msgend)>-1){
String head=key.substring(key.indexOf(booleanheadmsg)+8, key.lastIndexOf(msgend));
headmsg.put(head, value);
continue;
}
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(ptp.getParamname().equals(key)){
@ -240,18 +273,7 @@ public class InvokeMethod {
params.put(ptp.getParamname().replaceAll("&quot;", "\""), ptp.getParam().replaceAll("&quot;", "\"")); params.put(ptp.getParamname().replaceAll("&quot;", "\""), ptp.getParam().replaceAll("&quot;", "\""));
} }
} }
//´¦ÀíÍ·Óò
Map<String, String> headmsg = new HashMap<String, String>(0);
if(null!=ppt.getHeadmsg()&&!ppt.getHeadmsg().equals("")&&ppt.getHeadmsg().indexOf("=")>0){
String[] temp=ppt.getHeadmsg().split(";",-1);
for(int i=0;i<temp.length;i++){
if(null!=temp[i]&&!temp[i].equals("")&&temp[i].indexOf("=")>0){
String key=temp[i].substring(0, temp[i].indexOf("="));
String value=temp[i].substring(temp[i].indexOf("=")+1);
headmsg.put(key, value);
}
}
}
if (functionname.toLowerCase().equals("socketpost")) { if (functionname.toLowerCase().equals("socketpost")) {
result = HttpClientHelper.sendSocketPost(packagename, params, result = HttpClientHelper.sendSocketPost(packagename, params,
@ -315,6 +337,7 @@ public class InvokeMethod {
public static void main(String[] args) { public static void main(String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
} }

View File

@ -102,12 +102,14 @@ public class HttpClientHelper {
} }
} }
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (osw != null) { if (osw != null) {
try { try {
osw.close(); osw.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null; osw = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -121,6 +123,7 @@ public class HttpClientHelper {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -198,12 +201,14 @@ public class HttpClientHelper {
} }
} }
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (osw != null) { if (osw != null) {
try { try {
osw.close(); osw.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null; osw = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -212,6 +217,7 @@ public class HttpClientHelper {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -269,12 +275,14 @@ public class HttpClientHelper {
} }
os.flush(); os.flush();
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (os != null) { if (os != null) {
try { try {
os.close(); os.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
os = null; os = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -288,6 +296,7 @@ public class HttpClientHelper {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -347,12 +356,14 @@ public class HttpClientHelper {
resultBuffer.append(temp); resultBuffer.append(temp);
} }
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (br != null) { if (br != null) {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -417,12 +428,14 @@ public class HttpClientHelper {
resultBuffer.append(temp); resultBuffer.append(temp);
} }
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (br != null) { if (br != null) {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -471,12 +484,14 @@ public class HttpClientHelper {
resultBuffer.append(temp); resultBuffer.append(temp);
} }
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (br != null) { if (br != null) {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -526,12 +541,14 @@ public class HttpClientHelper {
resultBuffer.append(temp); resultBuffer.append(temp);
} }
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (br != null) { if (br != null) {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -559,6 +576,7 @@ public class HttpClientHelper {
try { try {
sbParams.append(URLEncoder.encode(String.valueOf(entry.getValue()), charset)); sbParams.append(URLEncoder.encode(String.valueOf(entry.getValue()), charset));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
sbParams.append("&"); sbParams.append("&");
@ -590,12 +608,14 @@ public class HttpClientHelper {
resultBuffer.append(temp); resultBuffer.append(temp);
} }
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (br != null) { if (br != null) {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -673,12 +693,14 @@ public class HttpClientHelper {
// 读取出响应体数据就是你要的数据 // 读取出响应体数据就是你要的数据
result = readLine(is, contentLength, charset); result = readLine(is, contentLength, charset);
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (osw != null) { if (osw != null) {
try { try {
osw.close(); osw.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null; osw = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -686,6 +708,7 @@ public class HttpClientHelper {
try { try {
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
socket = null; socket = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -696,6 +719,7 @@ public class HttpClientHelper {
try { try {
is.close(); is.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
is = null; is = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -704,6 +728,7 @@ public class HttpClientHelper {
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
socket = null; socket = null;
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -782,12 +807,14 @@ public class HttpClientHelper {
// 读取出响应体数据就是你要的数据 // 读取出响应体数据就是你要的数据
result = readLine(is, contentLength, charset); result = readLine(is, contentLength, charset);
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (osw != null) { if (osw != null) {
try { try {
osw.close(); osw.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null; osw = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -795,6 +822,7 @@ public class HttpClientHelper {
try { try {
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
socket = null; socket = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -805,6 +833,7 @@ public class HttpClientHelper {
try { try {
is.close(); is.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
is = null; is = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -812,6 +841,7 @@ public class HttpClientHelper {
try { try {
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
socket = null; socket = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -899,22 +929,26 @@ public class HttpClientHelper {
} }
// 读取返回内容 // 读取返回内容
resultBuffer = new StringBuffer(); resultBuffer = new StringBuffer();
responsecode = String.valueOf(con.getResponseCode()); responsecode = String.valueOf(con.getResponseCode());
int contentLength = Integer.parseInt(con.getHeaderField("Content-Length")); if(null!=con.getHeaderField("Content-Length")){
if (contentLength > 0) { int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));
br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset)); if (contentLength > 0) {
String temp; br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
while ((temp = br.readLine()) != null) { String temp;
resultBuffer.append(temp); while ((temp = br.readLine()) != null) {
resultBuffer.append(temp);
}
} }
} }
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (osw != null) { if (osw != null) {
try { try {
osw.close(); osw.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null; osw = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -928,6 +962,7 @@ public class HttpClientHelper {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
@ -944,9 +979,9 @@ public class HttpClientHelper {
/** /**
* @Description:ʹÓÃHttpClient·¢ËÍputÇëÇó * @Description:使用HttpClient发送put请求 参数JSON格式
*/ */
public static String httpClientPut(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg) { public static String httpClientPutJson(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg) {
StringBuffer resultBuffer = null; StringBuffer resultBuffer = null;
String responsecode = null; String responsecode = null;
CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpClient httpclient = HttpClients.createDefault();
@ -969,9 +1004,8 @@ public class HttpClientHelper {
try { try {
if(params.size()>0){ if(params.size()>0){
JSONObject jsonObject = JSONObject.fromObject(params); JSONObject jsonObject = JSONObject.fromObject(params);
System.out.println(jsonObject.toString()); StringEntity entity = new StringEntity(jsonObject.toString(),charset);
StringEntity entity = new StringEntity(jsonObject.toString(),charset); httpput.setEntity(entity);
httpput.setEntity(entity);
} }
CloseableHttpResponse response = httpclient.execute(httpput); CloseableHttpResponse response = httpclient.execute(httpput);
@ -988,12 +1022,76 @@ public class HttpClientHelper {
resultBuffer.append(temp); resultBuffer.append(temp);
} }
} catch (Exception e) { } catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
if (br != null) { if (br != null) {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null;
throw new RuntimeException(e);
}
}
}
return responsecode + resultBuffer.toString();
}
/**
* @Description:使用HttpClient发送put请求
*/
public static String httpClientPut(String urlParam, Map<String, Object> params, String charset,Map<String, String> headmsg) {
StringBuffer resultBuffer = null;
String responsecode = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPut httpput = new HttpPut(urlParam);
//替换头域信息
for (Map.Entry<String, String> m :headmsg.entrySet()) {
String key=m.getKey();
String value=m.getValue();
if(null!=value&&value.indexOf("Base64(")==0){
String valuesub=value.substring(value.indexOf("Base64(")+7,value.lastIndexOf(")"));
value="Basic " + DatatypeConverter.printBase64Binary((valuesub).getBytes());
httpput.setHeader(key,value);
}else{
httpput.setHeader(key,value);
}
}
// 构建请求参数
BufferedReader br = null;
try {
if(params.size()>0){
//拼接参数
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
for (Map.Entry<String, Object> m :params.entrySet()) {
nvps.add(new BasicNameValuePair(m.getKey(), m.getValue().toString()));
}
httpput.setEntity(new UrlEncodedFormEntity(nvps,charset));
}
CloseableHttpResponse response = httpclient.execute(httpput);
// 读取服务器响应数据
resultBuffer = new StringBuffer();
//获取请求对象中的响应行对象
org.apache.http.StatusLine statusLine = response.getStatusLine();
//从状态行中获取状态码
responsecode = String.valueOf(statusLine.getStatusCode());
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
String temp;
while ((temp = br.readLine()) != null) {
resultBuffer.append(temp);
}
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null; br = null;
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -9,9 +9,6 @@ import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.Properties; import java.util.Properties;
import luckyclient.publicclass.LogUtil;
import luckyclient.publicclass.SysConfig;
/** /**
* ================================================================= * =================================================================
* 这是一个受限制的自由软件您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途也不允许对程序代码修改后以任何形式任何目的的再发布 * 这是一个受限制的自由软件您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途也不允许对程序代码修改后以任何形式任何目的的再发布