增加httpclientpostxml方法

This commit is contained in:
seagull 2019-09-17 17:11:11 +08:00
parent 4776b5c90d
commit 8b2ab6798b
5 changed files with 160 additions and 48 deletions

View File

@ -1,35 +1,35 @@
package luckyclient.caserun.publicdispose.actionkeyword;
import luckyclient.driven.SubString;
import luckyclient.publicclass.LogUtil;
/**
* 动作关键字的处理接口的实现类截取测试结果指定开始及结束位置字符串
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="subcentrestr")
public class SubCentresStrActionParser implements ActionKeyWordParser {
/**
* 截取测试结果指定开始及结束位置字符串
* @param actionKeyWord 动作关键字
* @param testResult 测试结果
*/
@Override
public String parse(String actionParams, String testResult) {
String startstr="";
String endstr="";
if(actionParams.startsWith("[")&&actionParams.endsWith("]")){
startstr=actionParams.substring(actionParams.indexOf("[")+1, actionParams.indexOf("]"));
endstr=actionParams.substring(actionParams.lastIndexOf("[")+1, actionParams.lastIndexOf("]"));
testResult= SubString.subCentreStr(testResult, startstr, endstr);
LogUtil.APP.info("Action(subCentreStr):截取测试结果指定开始及结束位置字符串:{}",testResult);
}else{
testResult="步骤动作subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作参数:"+actionParams;
LogUtil.APP.warn("步骤动作subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作参数:{}",actionParams);
}
return testResult;
}
}
package luckyclient.caserun.publicdispose.actionkeyword;
import luckyclient.driven.SubString;
import luckyclient.publicclass.LogUtil;
/**
* 动作关键字的处理接口的实现类截取测试结果指定开始及结束位置字符串
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="subcentrestr")
public class SubCentresStrActionParser implements ActionKeyWordParser {
/**
* 截取测试结果指定开始及结束位置字符串
* @param actionKeyWord 动作关键字
* @param testResult 测试结果
*/
@Override
public String parse(String actionParams, String testResult) {
String startstr="";
String endstr="";
if(actionParams.startsWith("[")&&actionParams.endsWith("]")){
startstr=actionParams.substring(actionParams.indexOf("[")+1, actionParams.indexOf("]"));
endstr=actionParams.substring(actionParams.lastIndexOf("[")+1, actionParams.lastIndexOf("]"));
testResult= SubString.subCentreStr(testResult, startstr, endstr);
LogUtil.APP.info("Action(subCentreStr):截取测试结果指定开始及结束位置字符串:{}",testResult);
}else{
testResult="步骤动作subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作参数:"+actionParams;
LogUtil.APP.warn("步骤动作subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作参数:{}",actionParams);
}
return testResult;
}
}

View File

@ -401,15 +401,4 @@ public class SubString {
return jsonString;
}
public static void main(String[] args) {
String aa="$JP#\\=fd=sa#fdsdsaa\\#jjdj\\=s#ff";
aa = aa.substring("$JP#".length());
String[] bb = aa.split("(?<!\\\\)=");
System.out.println(bb[0]);
for(String s:bb){
s = s.replace("\\=","=");
System.out.println(s);
}
System.out.println(bb[1]);
}
}

View File

@ -239,6 +239,8 @@ public class InvokeMethod {
result = HttpClientHelper.httpClientPut(packagename, params, headmsg , ppt);
} else if (functionname.toLowerCase().equals("httpclientget")) {
result = HttpClientHelper.httpClientGet(packagename, params, headmsg, ppt);
} else if (functionname.toLowerCase().equals("httpclientpostxml")) {
result = HttpClientHelper.httpClientPostXml(packagename, params, headmsg, ppt);
} else {
LogUtil.APP.warn("您的HTTP操作方法异常检测到的操作方法是:{}",functionname);
result = "调用异常,请查看错误日志!";

View File

@ -16,7 +16,7 @@ public class SysConfig {
private static final String SYS_CONFIG_FILE = "/sys_config.properties";
static{
try {
InputStream in = new BufferedInputStream(AppiumConfig.class.getResourceAsStream(SYS_CONFIG_FILE));
InputStream in = new BufferedInputStream(SysConfig.class.getResourceAsStream(SYS_CONFIG_FILE));
SYS_CONFIG.load(new InputStreamReader(in, "UTF-8"));
} catch (IOException e) {
e.printStackTrace();

View File

@ -3,6 +3,7 @@ package luckyclient.publicclass.remoterinterface;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -52,6 +53,7 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
@ -1733,7 +1735,126 @@ public class HttpClientHelper {
return httpclient;
}
public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException {
/**
* 使用HttpClient以XML发送post请求
* @param urlParam
* @param params
* @param charset
* @param headmsg
* @param cerpath
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static String httpClientPostXml(String urlParam, Map<String, Object> params, Map<String, String> headmsg,ProjectProtocolTemplate ppt) throws NoSuchAlgorithmException, KeyManagementException {
String cerpath=ppt.getCerificatePath();
String charset=ppt.getEncoding().toLowerCase();
int timeout=ppt.getTimeout()*1000;
int responsehead=ppt.getIsResponseHead();
int responsecode=ppt.getIsResponseCode();
StringBuffer resultBuffer = null;
LogUtil.APP.info("设置HTTP请求地址:【{}】",urlParam);
CloseableHttpClient httpclient=iniHttpClient(urlParam,cerpath);
HttpPost httpPost = new HttpPost(urlParam);
httpPost.setHeader("Content-Type", "text/xml");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
//设置请求和传输超时时间
.setSocketTimeout(timeout).build();
httpPost.setConfig(requestConfig);
//替换头域信息
for (Map.Entry<String, String> m :headmsg.entrySet()) {
String key=m.getKey();
String value=m.getValue();
LogUtil.APP.info("开始设置|替换httpClientPostXml头域信息...key:【{}】 value:【{}】",key,value);
if(null!=value&&value.indexOf("Base64(")==0){
String valuesub=value.substring(value.indexOf("Base64(")+7,value.lastIndexOf(")"));
value="Basic " + DatatypeConverter.printBase64Binary((valuesub).getBytes());
LogUtil.APP.info("将头域【{}】的值【{}】FORMAT成BASE64格式...",key,value);
httpPost.setHeader(key, value);
}else{
httpPost.setHeader(key, value);
}
}
try {
if(params.size()>0){
if(1==params.size()&&params.containsKey("_forTextXml")){
LogUtil.APP.info("参数类型XML,设置httpClientPostXml参数信息...【{}】",params.get("_forTextXml").toString());
String xmlStr=getXmlString(params.get("_forTextXml").toString());
StringEntity entity = new StringEntity(xmlStr,charset);
entity.setContentType("text/xml");
httpPost.setEntity(entity);
}else{
String jsonString = JSON.toJSONString(params);
LogUtil.APP.info("参数类型FORM,设置httpClientPostXml参数信息...【{}】",jsonString);
StringEntity entity = new StringEntity(jsonString,charset);
httpPost.setEntity(entity);
}
}
CloseableHttpResponse response = httpclient.execute(httpPost);
// 读取服务器响应数据
resultBuffer = new StringBuffer();
if(1==responsehead){
Header[] headmsgstr=response.getAllHeaders();
resultBuffer.append("RESPONSE_HEAD:【{");
for(Header header:headmsgstr){
resultBuffer.append("\""+header.getName()+"\":\""+header.getValue()+"\",");
}
resultBuffer.delete(resultBuffer.length()-1, resultBuffer.length()).append("}】 ");
}
if(1==responsecode){
resultBuffer.append("RESPONSE_CODE:【"+response.getStatusLine().getStatusCode()+"");
}
if(null!=response.getEntity()){
HttpEntity entity = response.getEntity();
resultBuffer.append(EntityUtils.toString(entity));
}
} catch (Exception e) {
LogUtil.APP.error("使用HttpClient以XML发送post请求出现异常请检查", e);
throw new RuntimeException(e);
}
return resultBuffer.toString();
}
/**
* 读取xml内容,将请求的xml保存成字符串 进行post发送
* @param path
* @return
* @author Seagull
* @date 2019年9月17日
*/
private static String getXmlString(String path) {
StringBuilder sb = new StringBuilder();
// 构建请求参数
BufferedReader br = null;
try {
InputStream inputStream = new FileInputStream(path);
br = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
sb.append(line + "\n");
}
} catch (FileNotFoundException e) {
LogUtil.APP.error("此路径【"+path+"】下没有找到对应的XML文件", e);
} catch (IOException e) {
LogUtil.APP.error("读取文件失败,出现异常!", e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
LogUtil.APP.error("读取xml文件后关闭br流出现异常请检查", e);
br = null;
throw new RuntimeException(e);
}
}
}
return sb.toString();
}
}