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.PublicCaseParams;
import luckyclient.publicclass.ChangString;
import luckyclient.publicclass.DBOperation;
import luckyclient.publicclass.InvokeMethod;
/**
* =================================================================
@ -31,7 +30,7 @@ public class ApiTestCaseDebug{
* @param 项目名
* @param 用例编号
* @param 用例版本号
* 用于在testlink上配置好用例参数后做单条用例调试
* 用于在本地做单条用例调试
*/
public static void oneCaseDebug(String projectname,String testCaseExternalId){
Map<String,String> variable = new HashMap<String,String>(0);
@ -49,6 +48,11 @@ public class ApiTestCaseDebug{
variable.put(pcp.getParamsname(), pcp.getParamsvalue());
}
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++){
Map<String,String> casescript = InterfaceAnalyticCase.analyticCaseStep(testcaseob, steps.get(i),"888888",null);
@ -155,7 +159,7 @@ public class ApiTestCaseDebug{
* @param 项目名
* @param 用例编号
* @param 用例版本号
* 用于在testlink上配置好用例参数后做多条用例串行调试
* 用于在本地做多条用例串行调试
*/
public static void moreCaseDebug(String projectname,Map<String,Integer> addtestcase){
System.out.println(addtestcase.size());

View File

@ -56,6 +56,12 @@ public class TestCaseExecution {
variable.put(pcp.getParamsname(), pcp.getParamsvalue());
}
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++) {
Map<String, String> casescript = InterfaceAnalyticCase.analyticCaseStep(testcaseob, steps.get(i), taskid,caselog);
@ -167,21 +173,15 @@ public class TestCaseExecution {
}
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 + "解析成功,并成功调用用例中方法,请继续查看执行结果!");
LogOperation.updateCaseLogDetail(testCaseExternalId, taskid, "解析成功,并成功调用用例中方法,请继续查看执行结果!", "info",
"SETCASERESULT...");
// TCResult =
// TestCaseApi.setTCResult(projectname,testCaseExternalId, testnote,
// version,setresult);
caselog.updateCaseDetail(taskid, testCaseExternalId, setresult);
} else {
setresult = 1;
luckyclient.publicclass.LogUtil.APP.error("用例 " + testCaseExternalId + "解析或是调用步骤中的方法出错!");
LogOperation.updateCaseLogDetail(testCaseExternalId, taskid, "解析或是调用步骤中的方法出错!", "error", "SETCASERESULT...");
// TCResult =
// TestCaseApi.setTCResult(projectname,testCaseExternalId, testnote,
// version,2);
caselog.updateCaseDetail(taskid, testCaseExternalId, 2);
}
if (0 == setresult) {
@ -216,6 +216,12 @@ public class TestCaseExecution {
variable.put(pcp.getParamsname(), pcp.getParamsvalue());
}
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++) {
Map<String, String> casescript = InterfaceAnalyticCase.analyticCaseStep(testcaseob, steps.get(i), taskid,caselog);

View File

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

View File

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

View File

@ -86,15 +86,36 @@ public class InvokeMethod {
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")
List<ProjectTemplateParams> paramslist = JSONArray.toList(jsonarr, new ProjectTemplateParams(),
new JsonConfig());
//处理更换参数
if(null!=getParameterValues){
String booleanheadmsg="headmsg(";
String msgend=")";
for (Object obp : getParameterValues) {
String paramob = obp.toString();
String key = paramob.substring(0, paramob.indexOf("#"));
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++) {
ProjectTemplateParams ptp = paramslist.get(i);
if(ptp.getParamname().equals(key)){
@ -123,18 +144,6 @@ public class InvokeMethod {
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) {
if(ptp.getParam().indexOf("***[")>-1&&"***[".equals(ptp.getParam().substring(0, 4))){
@ -168,7 +177,10 @@ public class InvokeMethod {
} else if (functionname.toLowerCase().equals("httpurldelete")) {
result = HttpClientHelper.sendHttpURLDel(packagename, params,
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,
ppt.getContentencoding().toLowerCase(),headmsg);
} else if (functionname.toLowerCase().equals("httpclientget")) {
@ -204,15 +216,36 @@ public class InvokeMethod {
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")
List<ProjectTemplateParams> paramslist = JSONArray.toList(jsonarr, new ProjectTemplateParams(),
new JsonConfig());
//处理更换参数
if(null!=getParameterValues){
String booleanheadmsg="headmsg(";
String msgend=")";
for (Object obp : getParameterValues) {
String paramob = obp.toString();
String key = paramob.substring(0, action.indexOf("#"));
String value = paramob.substring(action.indexOf("#") + 1);
String key = paramob.substring(0, paramob.indexOf("#"));
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++) {
ProjectTemplateParams ptp = paramslist.get(i);
if(ptp.getParamname().equals(key)){
@ -240,18 +273,7 @@ public class InvokeMethod {
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")) {
result = HttpClientHelper.sendSocketPost(packagename, params,
@ -315,6 +337,7 @@ public class InvokeMethod {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}

View File

@ -102,12 +102,14 @@ public class HttpClientHelper {
}
}
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null;
throw new RuntimeException(e);
} finally {
@ -121,6 +123,7 @@ public class HttpClientHelper {
try {
br.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null;
throw new RuntimeException(e);
} finally {
@ -198,12 +201,14 @@ public class HttpClientHelper {
}
}
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null;
throw new RuntimeException(e);
}
@ -212,6 +217,7 @@ public class HttpClientHelper {
try {
br.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null;
throw new RuntimeException(e);
}
@ -269,12 +275,14 @@ public class HttpClientHelper {
}
os.flush();
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
os = null;
throw new RuntimeException(e);
} finally {
@ -288,6 +296,7 @@ public class HttpClientHelper {
try {
br.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null;
throw new RuntimeException(e);
} finally {
@ -347,12 +356,14 @@ public class HttpClientHelper {
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;
throw new RuntimeException(e);
} finally {
@ -417,12 +428,14 @@ public class HttpClientHelper {
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;
throw new RuntimeException(e);
}
@ -471,12 +484,14 @@ public class HttpClientHelper {
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;
throw new RuntimeException(e);
}
@ -526,12 +541,14 @@ public class HttpClientHelper {
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;
throw new RuntimeException(e);
}
@ -559,6 +576,7 @@ public class HttpClientHelper {
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("&");
@ -590,12 +608,14 @@ public class HttpClientHelper {
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;
throw new RuntimeException(e);
}
@ -673,12 +693,14 @@ public class HttpClientHelper {
// 读取出响应体数据就是你要的数据
result = readLine(is, contentLength, charset);
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null;
throw new RuntimeException(e);
} finally {
@ -686,6 +708,7 @@ public class HttpClientHelper {
try {
socket.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
socket = null;
throw new RuntimeException(e);
}
@ -696,6 +719,7 @@ public class HttpClientHelper {
try {
is.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
is = null;
throw new RuntimeException(e);
} finally {
@ -704,6 +728,7 @@ public class HttpClientHelper {
socket.close();
} catch (IOException e) {
socket = null;
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
@ -782,12 +807,14 @@ public class HttpClientHelper {
// 读取出响应体数据就是你要的数据
result = readLine(is, contentLength, charset);
} catch (Exception e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null;
throw new RuntimeException(e);
} finally {
@ -795,6 +822,7 @@ public class HttpClientHelper {
try {
socket.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
socket = null;
throw new RuntimeException(e);
}
@ -805,6 +833,7 @@ public class HttpClientHelper {
try {
is.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
is = null;
throw new RuntimeException(e);
} finally {
@ -812,6 +841,7 @@ public class HttpClientHelper {
try {
socket.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
socket = null;
throw new RuntimeException(e);
}
@ -899,22 +929,26 @@ public class HttpClientHelper {
}
// 读取返回内容
resultBuffer = new StringBuffer();
responsecode = String.valueOf(con.getResponseCode());
int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));
if (contentLength > 0) {
br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
String temp;
while ((temp = br.readLine()) != null) {
resultBuffer.append(temp);
responsecode = String.valueOf(con.getResponseCode());
if(null!=con.getHeaderField("Content-Length")){
int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));
if (contentLength > 0) {
br = new BufferedReader(new InputStreamReader(con.getInputStream(), 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 (osw != null) {
try {
osw.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
osw = null;
throw new RuntimeException(e);
} finally {
@ -928,6 +962,7 @@ public class HttpClientHelper {
try {
br.close();
} catch (IOException e) {
luckyclient.publicclass.LogUtil.APP.error(e.getMessage(), e);
br = null;
throw new RuntimeException(e);
} 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;
String responsecode = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
@ -968,10 +1003,9 @@ public class HttpClientHelper {
BufferedReader br = null;
try {
if(params.size()>0){
JSONObject jsonObject = JSONObject.fromObject(params);
System.out.println(jsonObject.toString());
StringEntity entity = new StringEntity(jsonObject.toString(),charset);
httpput.setEntity(entity);
JSONObject jsonObject = JSONObject.fromObject(params);
StringEntity entity = new StringEntity(jsonObject.toString(),charset);
httpput.setEntity(entity);
}
CloseableHttpResponse response = httpclient.execute(httpput);
@ -988,12 +1022,14 @@ public class HttpClientHelper {
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;
throw new RuntimeException(e);
}
@ -1002,6 +1038,68 @@ public class HttpClientHelper {
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;
throw new RuntimeException(e);
}
}
}
return responsecode + resultBuffer.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}

View File

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