首页 > 编程知识 正文

webservice接口测试工具,webservice接口开发简单例子

时间:2023-05-04 04:32:04 阅读:37011 作者:671

教程主要是发送Httpclient的一些方式,接收返回值,因为在公司也要一直去对接外面的公司,请求的数据五花八门,返回数据也是各种格式都有,因为也是公司的时候遇到请求问题,所以没有很详细的解答原理,就放个请求代码了

httppost请求发送from date x-www-form-urlencoded请求formdata数据webserviceasmxasmxwebservicewsdl1. http post请求(

publicstaticjsonobjectpostjsontojson (jsonobjectjson,String url ) try ) closeablehttpclienthttpclients=http clients.creats //格式类型的接收格式为jsonstringentityentity=new string entity (JSON.tostring )、' utf-8 ' ); //传递的格式为stringpublicstaticjsonobjectpostjsontojson (string JSON,String url ) stringentityentity=new string entity ) jstojson //请求标头http post.setheader (' content-type ',' application/json ' ); closeablehttpresponseresponse=http client.execute (http post ); httpentityhttpentity=response.get entity (; //返回格式为jsonjsonobjectback=JSON object.parse object (entity utils.tostring (http entity,' UTF-8 ' ),JSON object //返回值的格式为byte,通常用于接收流文件byte [ ] back=entity utils.to bytearray (entity )。 返回后退; }catch(exceptione ) {e.printStackTrace ); 返回空值; }2. from datex-www-form-urlencoded请求

/* * * from datex-www-form-urlencoded加入String为JSON * @ paramqrcodemsg * @ paramposturl * @ return */publicjsonobjjjjj String postURL,namevaluepair [ ]数据) try { postmethodpostmethod } post method=new post method (posturl; post method.setrequestheader (' content-type ',' application/x-www-form-urlencoded; charset=utf-8 '; //参数的设置,需要注意的是里面不传递空值。 空字符串//namevaluepair [ ] data={//newnamevaluepair (' rcpt no ',qrCodeMsg ) )/); post method.set requestbody (数据; org.Apache.com mons.http client.http client http client=new org.Apache.com mons.http client.http client (插入响应执行POST方法字符串格式的数据stringbasecode=post method.getresponsebodyasstring (; 返回json格式数据的jsonobjectback=JSON object.parse object (base code,JSONObject.class ); 返回后退; }catch(exceptione ) ({ //logger.info ) )请求异常) e.getMessage )、e ); throw new运行时执行(

e.getMessage()); } }

3 发送formdata数据

CloseableHttpClient httpClient = HttpClients.createDefault();HttpPost httpPoss = new HttpPost(Url);MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();//请求数据entityBuilder.addTextBody("billBatchCode", batchCode);entityBuilder.addTextBody("billNo", billNo);entityBuilder.addTextBody("contextPath", contextPath);entityBuilder.addTextBody("idCode", idCode);HttpEntity entity = entityBuilder.build();httpPoss.setEntity(entity);CloseableHttpResponse response = httpClient.execute(httpPoss);HttpEntity httpEntity = response.getEntity();//返回数据 resposeContent String resposeContent = EntityUtils.toString(httpEntity, "UTF-8");

4.webservice asmx文件请求 webservice 有两种文件请求方式首先先将asmx文件 webservice 请求以前没很多接触过,开始有个对接的的没仔细看接口文档的请求方式,导致一段时间无从下手,请求webservice 首先要去看asmx文件里面会有方法名和soapaction 传参的参数名,看文件直接看他们请求接口127.0.0.1:0000/aaaaa.asmx?wsdl 后面加上?wsdl 就可以看到一个文件 具体的看这个老哥的https://blog.csdn.net/qq_41712834/article/details/100073846?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-3&spm=1001.2101.3001.4242

public JSONObject webserviceJosn(String json,String method) { //获取webservice接口地址 String url =applicationConfig.getZjurlIp(); //获取域名地址,server定义的 String soapaction = "http://tempuri.org/"; Service service = new Service(); try { Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url); //设置要调用的方法 call.setOperationName(new QName(soapaction,"Businessxxxxxx")); call.addParameter(new QName(soapaction, "jsonValuexxx"), XMLType.XSD_STRING, ParameterMode.IN); //设置要返回的数据类型 call.setReturnType(new QName(soapaction,"Businessxxxxxx"),String.class); call.setReturnType( XMLType.XSD_STRING); call.setUseSOAPAction(true); call.setSOAPActionURI(soapaction+"BusinessElectInvoicexxxxx"); //调用方法并传递参数 String result = (String) call.invoke(new Object[]{json});// System.out.println("result is:::"+result); JSONObject Invoice = JSONObject.parseObject(result ); return Invoice ; } catch (ServiceException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } return null;}

5.webservice wsdl文件请求 wsdl文件和asmx有一点也不一样 首先还是来看wsdl文件 这里也要找老哥:
https://blog.csdn.net/qq_32447301/article/details/79204311
这个老哥主要是讲解怎么看wsdl文件的。
这时候又来了一个老哥 :
https://blog.csdn.net/qq_35124535/article/details/62226585?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control
这个老哥主要是讲怎么请求的

我的代码找不到了

post请求获取outputStream

public static byte[] doPostWithByte(String path, JSONObject json) { byte[] bytes = null; try { // 创建连接 URL url = new URL(path); // 添加 请求内容 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置http连接属性 connection.setDoOutput(true);// http正文内,因此需要设为true, 默认情况下是false; connection.setDoInput(true);// 设置是否从httpUrlConnection读入,默认情况下是true; connection.setRequestMethod("POST"); // 可以根据需要 提交 GET、POST、DELETE、PUT等http提供的功能 // connection.setUseCaches(false);//设置缓存,注意设置请求方法为post不能用缓存 // connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", " application/json");// 设定 请求格式 json,也可以设定xml格式的 connection.setRequestProperty("Accept-Charset", "utf-8"); // 设置编码语言 connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(json.toString()); out.flush(); out.close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = connection.getInputStream().read(buffer)) > -1) { baos.write(buffer, 0, len); } baos.flush(); connection.disconnect(); bytes = baos.toByteArray();// 返回数据 // 断开连接 } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bytes; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。