HttpClient对webserive测试

原文转自:https://blog.csdn.net/luozhuwang/article/details/21242487

继上一篇SoapUI对webservice进行功能测试,补充用httpclient对webservice进行测试,将返回的数据写到xml中便于查看

[java]  view plain  copy
  1. @Test  
  2.     public void test1() throws ClientProtocolException, IOException{  
  3.         String file_dir="./File";  
  4.         String file="./File/format_xml.xml";  
  5.           
  6.         //wsdl地址:  
  7.         String url="http://interfacegscapp.salesappcn.com/axis2/services/Ebusiness?wsdl";  
  8.         //输入的数据格式:  
  9.         String data="[{\"ID\":\"201403041008153769\",\"ProjectID\":\"123456\",\"companyNumber\":\"00000\",\"createtime_cus\":\"20140304100815\",\"mobile\":\"13800136103\",\"name\":\"ligang\",\"resouce\":\"1\"}]";  
  10.         //返回的xml数据  
  11.         String conResult =null;  
  12.           
  13.         //拼写URL  
  14.         String soapReuqest="<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:axis=\"http://ws.apache.org/axis2\">"  
  15.                 +"<soap:Header/>"  
  16.                 +"<soap:Body>"  
  17.                 +" <axis:transCus>"  
  18.                 +"<axis:url>"+data+"</axis:url>"  
  19.                 +"</axis:transCus>"  
  20.                 +"</soap:Body>"  
  21.                 +"</soap:Envelope>";  
  22.                   
  23.         //1.创建请求  
  24.         CloseableHttpClient httpclient = HttpClients.createDefault();  
  25.         //2.获取httppost   
  26.         HttpPost httppost = new HttpPost(url);  
  27.           
  28.         //3.然后把SOAP请求数据添加到postmethod方法中  
  29.         byte[] b =soapReuqest.getBytes("utf-8");  
  30.         InputStream is = new ByteArrayInputStream(b,0,b.length);  
  31.         InputStreamEntity reqEntity =new InputStreamEntity(is,b.length);          
  32.         httppost.setEntity(reqEntity);    
  33.           
  34.         HttpResponse response=httpclient.execute(httppost);  
  35.           
  36.         int statuscode=response.getStatusLine().getStatusCode();  
  37.         if(statuscode==200){  
  38.             System.out.println("调用成功!");  
  39.             //读返回数据,将数据写入到xml文件中  
  40.              conResult = EntityUtils.toString(response.getEntity());               
  41.              writeStrToFile.writeStrToFile(conResult,file_dir,file);  
  42.         }else{  
  43.             System.out.println("调用失败!错误码:"+statuscode);  
  44.         }  
  45.         //ReadXML.ReadXml(file);          
  46.         httpclient.close();  
  47.           
  48.     }  

封装类:将数据写xml中
[java]  view plain  copy
  1. /** 
  2.      * 先判断文件及目录是否存在; 
  3.      * 将String类型的字符串写入到xml中 
  4.      * */  
  5.     public static void writeStrToFile(String xml,String file_dir,String file){  
  6.         try {  
  7.             File outfile = new File(file);  
  8.             File Direct = new File(file_dir);  
  9.             //如果文件夹不存在则创建      
  10.             if  (!Direct .exists()  && !Direct .isDirectory())        
  11.             {         
  12.                 System.out.println("File不存在");    
  13.                 Direct .mkdir();      
  14.             } else     
  15.             {    
  16.                 System.out.println("File目录存在");    
  17.             }    
  18.               
  19.             if(!outfile.isFile()){  
  20.                 System.out.println("format_xml.xml创建成功");  
  21.                 outfile.createNewFile();  
  22.             }else{  
  23.                 System.out.println("format_xml.xml文件存在,删除并重新创建文件");  
  24.                 outfile.delete();  
  25.                 outfile.createNewFile();  
  26.             }  
  27.               
  28.             FileOutputStream fos = new FileOutputStream(new File(file));  
  29.             Writer os = new OutputStreamWriter(fos,"utf-8");  
  30.             os.write(xml);  
  31.             os.flush();  
  32.             fos.close();  
  33.               
  34.         } catch (FileNotFoundException e) {  
  35.             e.printStackTrace();  
  36.         } catch (UnsupportedEncodingException e) {  
  37.             e.printStackTrace();  
  38.         } catch (IOException e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.           
  42.     }  

返回来的结果:

[java]  view plain  copy
  1. 调用成功!  
  2. <?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"><soapenv:Body><ns:transCusResponse xmlns:ns="http://ws.apache.org/axis2"><ns:return>1</ns:return></ns:transCusResponse></soapenv:Body></soapenv:Envelope>  
  3. File目录存在  
  4. format_xml.xml文件存在,删除并重新创建文件  
  5. PASSED: test1  

生成的文件:


依赖包:testng、HttpClient(4.3)、dom4j

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值