• 自己写的粗糙的Excel数据驱动Http接口测试框架(一)


    1、excel用例:

    2、用例执行:

    @Test
    public void BindBank() throws Exception {
    String fileName = "src/main/java/com/datatrees/testcase/BindBank.xlsx";
    handleOk &= initData(userName,enmobile);
    Runner.execute(fileName, mobile);
    System.out.println("##############-》 测试结束");
    }

    3、Runner封装类
    package com.datatrees.framwork;

    import com.datatrees.httpHelp.HttpUtils2;
    import com.datatrees.httpHelp.TokenUtils;
    import com.datatrees.httpHelp.common;

    import java.text.SimpleDateFormat;
    import java.util.*;

    /**
    * Created by lyh on 17/3/24.
    */
    public class Runner {

    //无需登录
    // 执行一条用例,写入一次excel
    public static void execute(String filepath) throws Exception { //--target/classes目录下的文件路径
    List<TestCase> testCases = ExcelUtil.read(filepath);
    int row = 0;
    String respost="";
    //遍历测试用例
    for (TestCase testCase : testCases) {
    row++;
    //URL
    String url = common.pdl_uri + testCase.getPath();
    //参数maps
    List<String> params = new ArrayList();
    Map paramMaps = new HashMap();
    params.add(testCase.getFirstParam());
    params.add(testCase.getSecondParam());
    params.add(testCase.getThirdParam());
    params.add(testCase.getForthParam());
    params.add(testCase.getFifthParam());
    params.add(testCase.getParam_6());
    params.add(testCase.getParam_7());
    for (String param : params) {
    String[] apiParam;
    if (param.contains("-")) {
    //参数属性和值
    apiParam = param.split("-");
    paramMaps.put(apiParam[0], apiParam[1]);
    System.out.println(paramMaps);
    }
    if (!paramMaps.isEmpty()) {
    //调用http接口发送请求
    respost = HttpUtils2.sendHttpPost(url, paramMaps, "utf-8");

    System.out.print(respost);
    } else {
    //调用http接口发送请求
    respost = HttpUtils2.sendHttpPost(url, "utf-8");
    System.out.print(respost);
    }

    }
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    String executTime = df.format(new Date());
    //写入实际值到excel和判断
    CompareRes.compare2(filepath, testCase, respost,executTime,row);
    }
    }


    //需要登录token执行一条用例,写入一次excel
    public static void execute(String filepath,String mobile) throws Exception { //--target/classes目录下的文件路径
    List<TestCase> testCases = ExcelUtil.read(filepath);
    int row = 0;
    TokenUtils tokencode=new TokenUtils();
    UserTokenCode userTokenCode = tokencode.getTokenCode(mobile);


    //遍历测试用例
    for (TestCase testCase : testCases) {
    row++;
    //URL
    String url = common.pdl_uri + testCase.getPath();
    String respost="";
    //参数maps
    List<String> params = new ArrayList();

    Map paramMaps = new HashMap();
    params.add(testCase.getFirstParam());
    params.add(testCase.getSecondParam());
    params.add(testCase.getThirdParam());
    params.add(testCase.getForthParam());
    params.add(testCase.getFifthParam());
    params.add(testCase.getParam_6());
    params.add(testCase.getParam_7());

    for (String param : params) {
    String[] apiParam;
    if (param.contains("-")) {
    //参数属性和值
    apiParam = param.split("-");
    paramMaps.put(apiParam[0], apiParam[1]);

    System.out.println(paramMaps);
    }
    }
    paramMaps.put("userId", userTokenCode.getUserId());
    paramMaps.put("token", userTokenCode.getTokenCode());
    if (!paramMaps.isEmpty()) {
    //调用http接口发送请求
    respost = HttpUtils2.sendHttpPost(url, paramMaps, "utf-8");
    System.out.print(respost);
    } else {
    //调用http接口发送请求
    respost = HttpUtils2.sendHttpPost(url, "utf-8");
    System.out.print(respost);
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    String executTime = df.format(new Date());
    //写入实际值到excel和判断
    CompareRes.compare2(filepath, testCase, respost,executTime, row);
    }

    }
    //执行完全部测试用例后,将测试结果一次写入excel
    public static void execute2(String filepath) throws Exception { //--target/classes目录下的文件路径
    List<TestCase> testCases = ExcelUtil.read(filepath);
    int row = 0;

    //遍历测试用例
    for (TestCase testCase : testCases) {
    row++;
    //URL
    String url = common.pdl_uri + testCase.getPath();
    String respost;
    //参数maps
    List<String> params = new ArrayList();
    Map paramMaps = new HashMap();
    params.add(testCase.getFirstParam());
    params.add(testCase.getSecondParam());
    params.add(testCase.getThirdParam());
    params.add(testCase.getForthParam());
    params.add(testCase.getFifthParam());
    params.add(testCase.getParam_6());
    params.add(testCase.getParam_7());


    for (String param : params) {
    String[] apiParam;
    if (param.contains("-")) {
    //参数属性和值
    apiParam = param.split("-");
    paramMaps.put(apiParam[0], apiParam[1]);
    //
    System.out.println(paramMaps);
    }
    if (!paramMaps.isEmpty()) {
    //调用http接口发送请求
    respost = HttpUtils2.sendHttpPost(url, paramMaps, "utf-8");
    System.out.print(respost);
    } else {
    //调用http接口发送请求
    respost = HttpUtils2.sendHttpPost(url, "utf-8");
    System.out.print(respost);
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    String executTime = df.format(new Date());
    //实际值填入testcase对象
    testCase.setActResu(respost);
    testCase.setExecuteTime(executTime);
    //将测试结果和报错信息填入testcase
    CompareRes.compare(testCase);

    //将testCase中的实际值,测试结果和报错信息填入excel
    }
    //写入实际值到excel和判断
    row = 0;
    CompareRes.WriteResult(filepath, testCases, row);
    }
    }
    }
    4、结果比较类
    package com.datatrees.framwork;

    import com.alibaba.fastjson.JSONObject;
    import org.apache.log4j.Logger;

    import java.util.*;

    /**
    * Created by lyh on 17/3/23.
    */
    public class CompareRes {
    private static Logger logger = Logger.getLogger(com.datatrees.framwork.TestCase.class);

    //每执行一个用例,将测试结果相关数据填入一次excel
    public static void compare2(String filePath, TestCase testCase, String actResu,String executeTime, int row) {
    int actResuNo = 12;//测试结果在第13例
    int ExecuteTimeNo = 13;//对比结果在第14列
    int TestResNo = 14;//对比结果在第15列
    int CommentNo = 15;//报错信息在16列
    JSONObject actRes_jsonObject;
    ExcelUtil.writeCell(filePath, row, ExecuteTimeNo, executeTime);
    ExcelUtil.writeCell(filePath, row, actResuNo, actResu);
    testCase.setActResu(actResu);
    String actRes = testCase.getActResu();
    String expRes = testCase.getExpResu();
    Map<String, String> actdata_map = new HashMap<String, String>();
    Map<String, String> expdata_map = new HashMap<String, String>();
    if (actRes.startsWith("{")) {
    actRes_jsonObject = JSONObject.parseObject(actRes);
    JSONObject expRes_jsonObject = JSONObject.parseObject(expRes);
    String actErrorMsg = actRes_jsonObject.getString("errorMsg");
    String actCode = actRes_jsonObject.getString("code");
    String expErrorMsg = expRes_jsonObject.getString("errorMsg");
    String expCode = expRes_jsonObject.getString("code");
    String act_data = actRes_jsonObject.getString("data");
    String exp_data = expRes_jsonObject.getString("data");
    // if (act_data != null && act_data.startsWith("{")) {
    // actdata_map = JsonsUtil.convertJsonStrToMap(act_data.trim());
    // }
    // if (exp_data != null && act_data.startsWith("{")) {
    // expdata_map = JsonsUtil.convertJsonStrToMap(exp_data.trim());
    // }
    if (exp_data != null&&act_data!=null) {
    if (actErrorMsg.trim().equals(expErrorMsg.trim()) && actCode.trim().equals(expCode.trim()) && ((act_data.trim().equals(exp_data.trim())) || (actdata_map.size() == expdata_map.size()))) {
    ExcelUtil.writeCell(filePath, row, TestResNo, "Passed");
    } else if (actErrorMsg.trim().equals(expErrorMsg) == false) {
    ExcelUtil.writeCell(filePath, row, TestResNo, "Failed");
    ExcelUtil.writeCell(filePath, row, CommentNo, "expErrorMsg不相同");
    } else if (actCode.trim().equals(expCode) == false) {
    ExcelUtil.writeCell(filePath, row, TestResNo, "Failed");
    ExcelUtil.writeCell(filePath, row, CommentNo, "expCode不相同");
    } else if (act_data.trim().equals(exp_data) == false) {
    ExcelUtil.writeCell(filePath, row, TestResNo, "Failed");
    ExcelUtil.writeCell(filePath, row, CommentNo, "data不相同");
    }
    } else if (exp_data==null&&act_data==null){
    if (actErrorMsg.trim().equals(expErrorMsg) && actCode.trim().equals(expCode)) {
    ExcelUtil.writeCell(filePath, row, TestResNo, "Passed");
    } else if (actErrorMsg.trim().equals(expErrorMsg) == false) {
    ExcelUtil.writeCell(filePath, row, TestResNo, "Failed");
    ExcelUtil.writeCell(filePath, row, CommentNo, "expErrorMsg不相同");
    } else if (actCode.trim().equals(expCode) == false) {
    ExcelUtil.writeCell(filePath, row, TestResNo, "Failed");
    ExcelUtil.writeCell(filePath, row, CommentNo, "expCode不相同");
    }
    }else {
    ExcelUtil.writeCell(filePath, row, TestResNo, "Failed");
    ExcelUtil.writeCell(filePath, row, CommentNo, "返回结果错误1");
    }
    } else {
    ExcelUtil.writeCell(filePath, row, TestResNo, "Failed");
    ExcelUtil.writeCell(filePath, row, CommentNo, "返回结果错误");
    }
    }

    //将用例都执行完,再将结果一次全部填入excel
    public static void WriteResult(String filePath, List<TestCase> testCases, int row) {
    int actResuNo = 12;//测试结果在第13例
    int executeNo = 13;//对比结果在第14列
    int TestResNo = 14;//对比结果在第15列
    int CommentNo = 15;//报错信息在16列
    for (TestCase testcase : testCases) {
    row++;
    ExcelUtil.writeCell(filePath, row, actResuNo, testcase.getActResu());
    ExcelUtil.writeCell(filePath, row, executeNo, testcase.getExecuteTime());
    ExcelUtil.writeCell(filePath, row, TestResNo, testcase.getTestResult());
    ExcelUtil.writeCell(filePath, row, CommentNo, testcase.getComment());
    }
    }

    //实际值与预期值对比,并写入testcase对象
    public static void compare(TestCase testCase) {

    String actRes = testCase.getActResu();
    String expRes = testCase.getExpResu();
    JSONObject actRes_jsonObject;
    if (actRes.startsWith("{")) {
    actRes_jsonObject = JSONObject.parseObject(actRes);
    JSONObject expRes_jsonObject = JSONObject.parseObject(expRes);
    String actErrorMsg = actRes_jsonObject.getString("errorMsg");
    String actCode = actRes_jsonObject.getString("code");
    String act_data = actRes_jsonObject.getString("data");
    String expErrorMsg = expRes_jsonObject.getString("errorMsg");
    String expCode = expRes_jsonObject.getString("code");
    String exp_data = expRes_jsonObject.getString("data");

    if (actErrorMsg.trim().equals(expErrorMsg) && actCode.trim().equals(expCode) && act_data.trim().equals(exp_data)) {
    testCase.setTestResult("Passed");
    } else if (actErrorMsg.trim().equals(expErrorMsg) == false) {
    testCase.setTestResult("Failed");
    testCase.setComment("expErrorMsg不相同");
    } else if (actCode.trim().equals(expCode) == false) {
    testCase.setTestResult("Failed");
    testCase.setComment("expCode不相同");
    } else if (act_data.trim().equals(exp_data) == false) {
    testCase.setTestResult("Failed");
    testCase.setComment("data不相同");
    }
    } else {
    testCase.setTestResult("Failed");
    testCase.setComment("实际值与预期值不同");
    }
    }


    //断言优化
    public static void execute3(String filePath, TestCase testCase, String actResu, int row) {
    int actResuNo = 12;//测试结果在第13例
    int TestResNo = 13;//对比结果在第14列
    int CommentNo = 14;//报错信息在15列
    ExcelUtil.writeCell(filePath, row, actResuNo, actResu);
    testCase.setActResu(actResu);
    String actRes = testCase.getActResu();
    String expRes = testCase.getExpResu();
    String caseName = testCase.getSubID();
    AssertResult.AssertFunction(caseName, expRes, actRes, filePath, row);
    }

    }

    5、excelUtil类:
    package com.datatrees.framwork;

    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.math.BigDecimal;
    import java.util.ArrayList;
    import java.util.List;

    /**
    * Created by lyh on 17/3/23.
    */
    public class ExcelUtil {
    //读取Excel中数据
    public static List<TestCase> read(String fileName) throws Exception {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet s = wb.createSheet();
    XSSFRow row = s.createRow(0);
    XSSFCell cell = row.createCell((int) 0, 0);
    //------------从xls读出数据
    // fileName = ClassLoader.getSystemResource("").toString().replace("file:", "").replace("target/classes/", "").trim() + fileName;

    wb = new XSSFWorkbook(new FileInputStream(fileName));

    s = wb.getSheetAt(0);

    //获得EXCEL行数
    int rowNums = s.getLastRowNum();
    //获得Excell列数
    //int columnNum=r.getPhysicalNumberOfCells();

    List<TestCase> testCases = new ArrayList<TestCase>();
    for (int i = 1; i <= rowNums; i++) {
    XSSFRow r = s.getRow(i);
    // cell = r.getCell(0);
    TestCase testCase = new TestCase();

    testCase.setID(getCellValue(r.getCell(0)));
    testCase.setSubID(r.getCell(1).getStringCellValue());
    testCase.setCaseDes(r.getCell(2).getStringCellValue());
    testCase.setPath(r.getCell(3).getStringCellValue());

    testCase.setFirstParam(r.getCell(4).getStringCellValue());
    testCase.setSecondParam(r.getCell(5).getStringCellValue());
    testCase.setThirdParam(r.getCell(6).getStringCellValue());
    testCase.setForthParam(r.getCell(7).getStringCellValue());
    testCase.setFifthParam(r.getCell(8).getStringCellValue());
    testCase.setParam_6(r.getCell(9).getStringCellValue());
    testCase.setParam_7(r.getCell(10).getStringCellValue());

    testCase.setExpResu(r.getCell(11).getStringCellValue());
    testCase.setActResu(r.getCell(12).getStringCellValue());
    testCase.setExecuteTime(r.getCell(13).getStringCellValue());
    testCase.setTestResult(r.getCell(14).getStringCellValue());
    testCase.setComment(r.getCell(15).getStringCellValue());
    // System.out.println(cell.getRichStringCellValue());
    testCases.add(testCase);

    }
    return testCases;

    }

    /**
    * 写入Excel,在任意坐标处写入数据。
    * String value:你要输入的内容
    * int x :行坐标,Excel从 0 算起
    * int y :列坐标,Excel从 0 算起
    */
    public static void writeCell(String filePath, int x, int y, String value) {
    try {
    // 创建Excel的工作书册 Workbook,对应到一个excel文档
    XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(filePath));
    XSSFSheet sheet = wb.getSheetAt(0);
    XSSFRow row = sheet.getRow(x);
    XSSFCell cell = row.getCell((short) y);
    cell.setCellValue(value);
    FileOutputStream os;
    os = new FileOutputStream(filePath);
    wb.write(os);
    os.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    /**
    * 取表格值
    *
    * @param xssfCell XSSFCell类型对象,表示单元格。
    * @return:返回单元格数值
    */

    public static String getCellValue(XSSFCell xssfCell) {
    if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) {
    return String.valueOf(xssfCell.getBooleanCellValue());
    } else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) {
    return new BigDecimal(xssfCell.getNumericCellValue()).toPlainString();
    // return String.valueOf(xssfCell.getNumericCellValue());
    } else {
    return String.valueOf(xssfCell.getStringCellValue());
    }

    }
    }
    6、用例类:
    package com.datatrees.framwork;

    /**
    * Created by lyh on 17/3/23.
    */
    public class TestCase {
    String ID;//编号
    String SubID;//姓名
    String CaseDes;//年龄
    String Path;//路径
    String FirstParam;//第一个参数
    String SecondParam;//第二个参数
    String ThirdParam;//第三个参数
    String ForthParam;//第四个参数
    String FifthParam;//第五个参数
    String Param_6;//第6个参数
    String Param_7;//第7个参数

    String expResu;//期望结果
    String actResu;//实际结果
    String executeTime;//执行时间
    String TestResult;//是否通过
    String Comment;//描述


    public TestCase() {
    }

    public String getExecuteTime() {
    return executeTime;
    }

    public void setExecuteTime(String executeTime) {
    this.executeTime = executeTime;
    }
    public String getParam_7() {
    return Param_7;
    }

    public void setParam_7(String param_7) {
    Param_7 = param_7;
    }
    public String getParam_6() {
    return Param_6;
    }

    public void setParam_6(String param_6) {
    Param_6 = param_6;
    }

    public String getID() {
    return ID;
    }

    public void setID(String ID) {
    this.ID = ID;
    }
    public String getSubID() {
    return SubID;
    }

    public void setSubID(String subID) {
    SubID = subID;
    }

    public String getCaseDes() {
    return CaseDes;
    }

    public void setCaseDes(String caseDes) {
    CaseDes = caseDes;
    }

    public String getPath() {
    return Path;
    }

    public void setPath(String path) {
    Path = path;
    }

    public String getFirstParam() {
    return FirstParam;
    }

    public void setFirstParam(String firstParam) {
    FirstParam = firstParam;
    }

    public String getSecondParam() {
    return SecondParam;
    }

    public void setSecondParam(String secondParam) {
    SecondParam = secondParam;
    }

    public String getThirdParam() {
    return ThirdParam;
    }

    public void setThirdParam(String thirdParam) {
    ThirdParam = thirdParam;
    }

    public String getForthParam() {
    return ForthParam;
    }

    public void setForthParam(String forthParam) {
    ForthParam = forthParam;
    }

    public String getFifthParam() {
    return FifthParam;
    }

    public void setFifthParam(String fifthParam) {
    FifthParam = fifthParam;
    }

    public String getExpResu() {
    return expResu;
    }

    public void setExpResu(String expResu) {
    this.expResu = expResu;
    }

    public String getActResu() {
    return actResu;
    }

    public void setActResu(String actResu) {
    this.actResu = actResu;
    }

    public String getTestResult() {
    return TestResult;
    }

    public void setTestResult(String testResult) {
    TestResult = testResult;
    }

    public String getComment() {
    return Comment;
    }

    public void setComment(String comment) {
    Comment = comment;
    }
    }

    7、http工具类:
    package com.datatrees.httpHelp;

    import com.alibaba.fastjson.JSONObject;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;

    /**
    * Created by lyh on 17/3/16.
    */
    public class HttpUtils2 {
    private static Logger logger = LoggerFactory.getLogger(HttpUtils2.class);
    private static CloseableHttpClient httpClient = HttpClients.createDefault();


    public String sendHttpGet(String url) {
    HttpGet httpGet = new HttpGet(url);
    String result = "fail";
    try {
    CloseableHttpResponse response = httpClient.execute(httpGet);
    if (response.getStatusLine().getStatusCode() == 200) {
    result = EntityUtils.toString(response.getEntity());
    logger.debug(result);
    }
    } catch (ClientProtocolException e) {
    logger.error(e.toString());
    } catch (IOException e) {
    logger.error(e.toString());
    }
    return result;
    }

    public static String sendHttpPost(String url, JSONObject json, String charset) {
    HttpPost httppost = new HttpPost(url);
    httppost.setHeader("Content-type", "application/json");
    httppost.setHeader("Accept", "application/json;charset=UTF-8");
    String result = "fail";
    try {
    StringEntity entity = new StringEntity(json.toString(), charset);
    entity.setContentEncoding("UTF-8");
    httppost.setEntity(entity);
    HttpResponse response = httpClient.execute(httppost);
    System.out.println(response.getStatusLine().getStatusCode());
    if (response.getStatusLine().getStatusCode() == 200) {
    //读返回数据
    result = EntityUtils.toString(response.getEntity());
    }
    } catch (ClientProtocolException e) {
    e.printStackTrace();
    } catch (IOException e) {
    logger.error(e.toString());
    }
    return result;
    }

    public static String sendHttpPost(String url, Map<String, String> map, String charset) {
    logger.info(url);
    System.out.print("url:"+url);
    HttpPost httpPost = new HttpPost(url);

    String result = null;

    try {
    //设置参数
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    Iterator iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
    Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
    list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
    }
    if (list.size() > 0) {
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
    httpPost.setEntity(entity);
    }
    HttpResponse response = httpClient.execute(httpPost);
    if (response != null) {
    HttpEntity resEntity = response.getEntity();
    if (resEntity != null) {
    result = EntityUtils.toString(resEntity, charset);
    }
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return result;
    }

    public static String sendHttpPost2(String url, Map<String, String> map, String charset) {
    HttpPost httpPost = new HttpPost(url);
    String result = null;

    try {
    //设置参数
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    Iterator iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
    Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
    list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
    }
    if (list.size() > 0) {
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
    httpPost.setEntity(entity);
    }
    HttpResponse response = httpClient.execute(httpPost);
    if (response != null) {
    HttpEntity resEntity = response.getEntity();
    if (resEntity != null) {
    result = EntityUtils.toString(resEntity, charset);
    }
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return result;
    }

    public static String sendHttpPost(String url, List<NameValuePair> list, String charset) {
    HttpPost httpPost = new HttpPost(url);
    String result = null;
    try {
    if (list.size() > 0) {
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
    httpPost.setEntity(entity);
    }
    HttpResponse response = httpClient.execute(httpPost);
    if (response != null) {
    HttpEntity resEntity = response.getEntity();
    if (resEntity != null) {
    result = EntityUtils.toString(resEntity, charset);
    }
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return result;
    }
    public static String sendHttpPost(String url, String charset) {
    HttpPost httpPost = new HttpPost(url);
    String result = null;
    try {
    HttpResponse response = httpClient.execute(httpPost);
    if (response != null) {
    HttpEntity resEntity = response.getEntity();
    if (resEntity != null) {
    result = EntityUtils.toString(resEntity, charset);
    }
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return result;
    }
    public static String sendHttpPost(String url) {
    HttpPost httpPost = new HttpPost(url);
    String result = null;
    try {
    HttpResponse response = httpClient.execute(httpPost);
    if (response != null) {
    HttpEntity resEntity = response.getEntity();
    if (resEntity != null) {
    result = EntityUtils.toString(resEntity, "utf-8");
    }
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return result;
    }
    }











  • 相关阅读:
    一行命令搞定node.js 版本升级
    doesn't contain a valid partition table 解决方法
    debian kill 进程等命令
    FastDFS配置说明(中英文)
    FastDFS问题汇总
    FastDFS常见命令
    FastDFS安装配置手册
    windows 与Linux 互传文件
    FtpClient中文乱码问题解决
    windows 配置host
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/6673887.html
Copyright © 2020-2023  润新知