最近发现之前做过的东西,在未掌握牢固的情况下,很快就会忘掉,所以,通过这些东西来保留下一些测试方法,以便以后能够回顾。

    WebService的最后一种测试方法,也就是通过HTTP协议来手写脚本完成测试,这里虽说手写脚本,其实完成的大部分还是复制粘贴的工作,很简单,下面简要记录。

    这种方式和soap_request一样,都是找到接口文档的请求,利用这些请求书写函数。

    首先,LR新建HTTP协议的脚本。

    第二,脚本中用web_custom_request();函数,其中参数url为我们的天气预报的地址:http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx

    Method=POST,Body里面即是我们的请求,最终代码如下:

 

   web_custom_request("web_custom_request",
  "URL=http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx",
  "Method=POST",
  "TargetFrame=",
  "Resource=0",
  "Referer=",
  "Body=<?xml version=\"1.0\" encoding=\"utf-8\"?><soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">   <soap12:Body>    <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">        <theCityName>&#x4E0A;&#x6D77;</theCityName>    </getWeatherbyCityName>   </soap12:Body></soap12:Envelope>",
  LAST);

这就完成了我们的web_custom_request()函数

这时我们进行回访,脚本中会产生错误:

Action.c(17): Error -26616: HTTP Status-Code=415 (Unsupported Media Type) for
"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx" [MsgId: MERR-26616]

 

产生这个错误的原因是我们发送的数据包没有明确格式类型,服务器在校验格式时出现错误,所以我们需要手动添加一个Header头部分,使用函数web_add_header()添加web_add_header("Content-Type",
  "application/soap+xml; charset=utf-8");

这样再次运行就可以成功了。

 

细心的同学可能会发现在<theCityName>&#x4E0A;&#x6D77;</theCityName>

这里明明是填写城市的地方,为什么填的是一堆乱码呢。

这是由于我们发送的格式是utf-8格式的,那么在发送城市名字时也需要是utf-8编码的,要不然服务器是无法识别的,至于转换utf-8编码,可以通过百度搜索utf-8编码转换即可。

 

这就是LR测试webservice的最后一种方式,到现在3种方式已经完毕。