接口测试Java、python分别实现

阅读: 评论:0

接口测试Java、python分别实现

接口测试Java、python分别实现

免token  post请求方式
说明:以下代码实现中是根据我自己的业务逻辑实现,请结合自身实际情况参考使用。

python代码实现:

from ast import For
from email import header
s import codes
from time import sleep
from wsgiref.headers import Headers
import requests
import json
from urllib.parse import urlencode
import os
url = ''
headers = {"Content-type":"application/json"}
codesList = []
count = 1
with open(file='F:/Code/VSCode/PythonCode/muyuan/',mode='r',encoding='utf-8') as d:file = d.readlines()for line in file:if count % 1000 == 1:codesList.append(','.join(codes))codes = []codes.append(str(line).strip('n'))
wr = open(file='F:/Code/VSCode/PythonCode/',mode='a+',encoding='utf-8')for codesStr in codesList:data = {"jobNos": codesStr}r = requests.post(url=url,headers=headers,data=json.dumps(data))for t in r.json()['data']:print(f'{t["jobNo"]},{t["leaveDate"]}')wr.write(f'{t["jobNo"]},{t["leaveDate"]}n')wr.flushwr.flushr.closesleep(0.5)

Java代码实现:


import java.io.*;
import java.HttpURLConnection;
import java.URL;
import java.util.*;import com.alibaba.fastjson.JSONArray;
import org.apachemons.lang.StringUtils;
import com.alibaba.fastjson.JSONObject;/*** @Author: 魏一鸣* @Description:* @Date * @Modified By:*/
public class HttpPost1 {public static void main1() {String srcPath = "";String destPath ="";BufferedWriter bw = null;try {bw = new BufferedWriter(new FileWriter(destPath));} catch (IOException e) {throw new RuntimeException(e);}ArrayList<String> codesList = mainRead(srcPath);for (String codes : codesList) {String data = "{"+""jobNos":"+" ""+codes+""}";JSONObject jsonObject = JSONObject.parseObject(data);
//			System.out.println("jsonObject="&#String());String url = "";try {String s = httpJson(10000, url, data);System.out.println("s="+s);JSONObject resJson = JSONObject.parseObject(s);JSONArray resJsonJSONArray = JSONArray("data");for (Object o : resJsonJSONArray) {JSONObject dataJson = JSONObject.String());String result = ("jobNo").toString()+","&#("leaveDate").toString();System.out.println(result);
//					mainWrite(destPath,result);bw.write(result+"n");bw.flush();}} catch (Exception e) {throw new RuntimeException(e);}}}//ms超时毫秒,url地址,json入参public static String httpJson(int ms,String url,String json) throws Exception{String err = "00", line = null;StringBuilder sb = new StringBuilder();HttpURLConnection conn = null;BufferedWriter out = null;BufferedReader in = null;try{conn = (HttpURLConnection) (new placeAll("/","/"))).openConnection();conn.setRequestMethod("POST");conn.setDoOutput(true);conn.setDoInput(true);conn.setUseCaches(false);conn.setConnectTimeout(ms);conn.setReadTimeout(ms);conn.setRequestProperty("Content-Type","application/json;charset=utf-8");t();out = new BufferedWriter(new OutputStream(),"utf-8"));out.write(new Bytes(), "utf-8"));out.flush();//发送参数int code = ResponseCode();if (ResponseCode()==200){in = new BufferedReader(new InputStream(),"UTF-8"));while ((line&#adLine())!=null)sb.append(line);}//接收返回值}catch(Exception ex){err&#Message();}try{ if (out!=null) out.close(); }catch(Exception ex){};try{ if (in!=null) in.close(); }catch(Exception ex){};try{ if (conn!=null) conn.disconnect();}catch(Exception ex){}if (!err.equals("00")) throw new Exception(err);String();}public static ArrayList<String> mainRead(String srcPath) {try {BufferedReader br = new BufferedReader(new FileReader(srcPath));//构造一个BufferedReader类来读取文件String s = null;ArrayList<String> list = new ArrayList<>();
//			String[] str = {};ArrayList<String> codesList = new ArrayList<>();while((s = br.readLine())!=null){//使用readLine方法,一次读一行list.add(s);if (list.size()%2000 == 0) {Object[] array = Array();String join = StringUtils.join(array, ",");codesList.add(join);list = new ArrayList<>();}
//				System.out.println("===========================================");//				System.out.println("===========================================");}return codesList;} catch (IOException e) {throw new RuntimeException(e);}}public static void mainWrite(String destPath,String result) {try {BufferedWriter bw = new BufferedWriter(new FileWriter(destPath));bw.write(result+"n");bw.flush();} catch (IOException e) {throw new RuntimeException(e);}}}

本文发布于:2024-02-01 21:17:57,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170679347739480.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:接口   测试   python   Java
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23