51Testing软件测试论坛

标题: 接口测试相关问题(java+testng+selenium) [打印本页]

作者: 菜鸟小丸子    时间: 2017-9-14 11:11
标题: 接口测试相关问题(java+testng+selenium)
自动化新人一枚,最近在研究接口自动化,使用的是java+testng+selenium框架,请求是post请求,消息提示json格式的,目前请求拼出来了,但是不知道怎么把请求发送到url,然后去得到响应,上网查了一些资料,有很多是说用HttpURLConnection和httpclient模拟post请求,但是这两个我都不太懂,有没有大神能够指导一下?多谢!!!目前把请求拼出来如下:后面不会操作了
JSONObject object = new JSONObject();
                   ReqestHead reqHead = new ReqestHead();
                reqHead.setChannelId("ZT");
                reqHead.setRequestTime(DateUtil.getCurrentTime());
                reqHead.setAreaCode("025");
                reqHead.setTranId(DateUtil.getNowDate(DateUtil.FORMAT_1)+"123456");
                reqHead.setAuth(MD5Util.encryptByMD5(reqHead.getTranId()+"888888"));          
                          
                JSONObject body = new JSONObject();
                body.put("goodsNbr", "test20150623000926");
                body.put("qrGoodsType", "0");
                
                object.put("head", reqHead);
                object.put("body", body);
               String requestion=object.toJSONString();
作者: seagull1985    时间: 2017-9-14 13:41
  1.         /**
  2.          * @Description:使用HttpURLConnection发送post请求
  3.          */
  4.         public static String sendHttpURLPost(String urlParam, Map<String, Object> params, String charset, int timeout) {
  5.                 StringBuffer resultBuffer = null;
  6.                 // 构建请求参数
  7.                 StringBuffer sbParams = new StringBuffer();
  8.                 if (params != null && params.size() > 0) {
  9.                         for (Entry<String, Object> e : params.entrySet()) {
  10.                                 sbParams.append(e.getKey());
  11.                                 sbParams.append("=");
  12.                                 sbParams.append(e.getValue());
  13.                                 sbParams.append("&");
  14.                         }
  15.                 }
  16.                 HttpURLConnection con = null;
  17.                 OutputStreamWriter osw = null;
  18.                 BufferedReader br = null;
  19.                 // 发送请求
  20.                 try {
  21.                         URL url = new URL(urlParam);
  22.                         con = (HttpURLConnection) url.openConnection();
  23.                         con.setRequestMethod("POST");
  24.                         con.setDoOutput(true);
  25.                         con.setDoInput(true);
  26.                         con.setUseCaches(false);
  27.                         con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  28.                         con.setConnectTimeout(timeout*1000);
  29.                         if (sbParams != null && sbParams.length() > 0) {
  30.                                 osw = new OutputStreamWriter(con.getOutputStream(), charset);
  31.                                 osw.write(sbParams.substring(0, sbParams.length() - 1));
  32.                                 osw.flush();
  33.                         }
  34.                         // 读取返回内容
  35.                         resultBuffer = new StringBuffer();
  36.                         int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));
  37.                         if (contentLength > 0) {
  38.                                 br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
  39.                                 String temp;
  40.                                 while ((temp = br.readLine()) != null) {
  41.                                         resultBuffer.append(temp);
  42.                                 }
  43.                         }
  44.                 } catch (Exception e) {
  45.                         throw new RuntimeException(e);
  46.                 } finally {
  47.                         if (osw != null) {
  48.                                 try {
  49.                                         osw.close();
  50.                                 } catch (IOException e) {
  51.                                         osw = null;
  52.                                         throw new RuntimeException(e);
  53.                                 } finally {
  54.                                         if (con != null) {
  55.                                                 con.disconnect();
  56.                                                 con = null;
  57.                                         }
  58.                                 }
  59.                         }
  60.                         if (br != null) {
  61.                                 try {
  62.                                         br.close();
  63.                                 } catch (IOException e) {
  64.                                         br = null;
  65.                                         throw new RuntimeException(e);
  66.                                 } finally {
  67.                                         if (con != null) {
  68.                                                 con.disconnect();
  69.                                                 con = null;
  70.                                         }
  71.                                 }
  72.                         }
  73.                 }

  74.                 return resultBuffer.toString();
  75.         }
复制代码

作者: seagull1985    时间: 2017-9-14 13:42
  1. /**
  2.          * @Description:使用HttpClient发送post请求
  3.          */
  4.         public static String httpClientPost(String urlParam, Map<String, Object> params, String charset) {
  5.                 StringBuffer resultBuffer = null;
  6.                 HttpClient client = new DefaultHttpClient();
  7.                 HttpPost httpPost = new HttpPost(urlParam);
  8.                 // 构建请求参数
  9.                 List<NameValuePair> list = new ArrayList<NameValuePair>();
  10.                 Iterator<Entry<String, Object>> iterator = params.entrySet().iterator();
  11.                 while (iterator.hasNext()) {
  12.                         Entry<String, Object> elem = iterator.next();
  13.                         list.add(new BasicNameValuePair(elem.getKey(), String.valueOf(elem.getValue())));
  14.                 }
  15.                 BufferedReader br = null;
  16.                 try {
  17.                         if (list.size() > 0) {
  18.                                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
  19.                                 httpPost.setEntity(entity);
  20.                         }
  21.                         HttpResponse response = client.execute(httpPost);
  22.                         // 读取服务器响应数据
  23.                         resultBuffer = new StringBuffer();

  24.                         //br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  25.                         br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
  26.                         String temp;
  27.                         while ((temp = br.readLine()) != null) {
  28.                                 resultBuffer.append(temp);
  29.                         }
  30.                 } catch (Exception e) {
  31.                         throw new RuntimeException(e);
  32.                 } finally {
  33.                         if (br != null) {
  34.                                 try {
  35.                                         br.close();
  36.                                 } catch (IOException e) {
  37.                                         br = null;
  38.                                         throw new RuntimeException(e);
  39.                                 }
  40.                         }
  41.                 }
  42.                
  43.                 return resultBuffer.toString();
  44.         }
复制代码

作者: 菜鸟小丸子    时间: 2017-9-14 15:21
seagull1985 发表于 2017-9-14 13:41

多谢大神!能请教一下:Map<String, Object> params, String charset  这两个参数是什么吗?Map<String, Object> params是请求吗?
                object.put("head", reqHead);
                object.put("body", body);
       String requestion=object.toJSONString();
               String url="http://192.168.189.134:10000/centrality-client/usc/goodsDetailProvider/queryGoodsDetail";
               
               ReqPost.sendHttpURLPost(url, requestion, charset, 2);
作者: seagull1985    时间: 2017-9-15 10:15
菜鸟小丸子 发表于 2017-9-14 15:21
多谢大神!能请教一下:Map params, String charset  这两个参数是什么吗?Map params是请求吗?
              ...

是请求参数跟编码格式
作者: 菜鸟小丸子    时间: 2017-9-15 15:40
seagull1985 发表于 2017-9-15 10:15
是请求参数跟编码格式

多谢!!!!
作者: caind123    时间: 2019-5-7 11:52
可以了吗?




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2