全网封神最新最全:用最通俗易懂的方式来讲解http,WebService,Dubbo接口协议接口测试

38 篇文章 23 订阅
1 篇文章 0 订阅

目录

一、接口

1)接口分类

2)软件接口分类

3)常见的接口协议

4)接口测试

二、HTTP协议解读

1)http协议:超文本传输协议

2)https:简单的来说,就是http的安全版,在http下加入了SSL层(SSL主要用户web的安全传输协议)

3)http的默认端口号是:80 ,默认的端口在url可以不加

4)HTTP请求过程

5)HTTP请求信息

6)HTTP响应信息

7)HTTP响应状态码

8)HTTP请求方法

【WebService协议】

一. WSDL WebService的创建:1.创建【Web Service Project】:

2.写一个简单的测试用例:

3.发布Web Service:点击工具栏的New Web Service:

二. WSDL WebService的调用:方法1:创建Web Service Client来调用:1.创建【Java Project】:

2.点击工具栏的New Web Service Client:

3.选择【WSDL URL】:

4.点击【Next】完成创建后,在src/com/webservice下,自动生成相关文件。(WebServiceTest.java除外,这个是自己创建的调用文件)

5.创建【WebServiceTest.java】

6.【WebServiceTest.java】右键→Run As→Java Application输出结果:Welcome to use WebService, sun

【Dubbo 接口测试方法]

一.直接通telnet然后用dubbo协议调用方法

(1)在项目的配置文件中可以看到

用ls查看服务

查看服务下有那些方法ls -l

然后使用invoke测试接口,这里注意,pom需要添加 fastjson的依赖不然报Invalid json argument, cause: com/alibaba/fastjson/JSON dubbo

二.Idea 插件dubbo Invoke

下载安装插件DubboInvoke

在对外暴漏的方法上加注释

调用,点击这个接口出现的地方


一、接口

API: Application Programming Interface, 应用程序可编程接口

1)接口分类

硬件接口:具有连接功能、适配。两个硬件设备之间的连接方式(比如鼠标和电脑通过USB接口连接)

软件接口:软件程序之间数据交互的通道(用户界面是软件接口)

2)软件接口分类

程序内部接口:是客户端与服务器的接口,用来实现客户端和服务器的数据传递

外部接口:比如通过第三方登录,第三方支付,通过调用外部接口并返回当前的系统

3)常见的接口协议

webService接口:使用soup协议通过http传输,请求报文和返回报文都是xml格式的,常用测试工具有soupUI

http协议接口:目前使用最广泛的,使用HTTP协议来传输数据,常见的请求方法有get、post等,常用测试工具有postman、jmeter

dubbo、 websocket、 ws://...、 ftp://等协议。

4)接口测试

本质是基于某种协议,发送一个请求给服务器,然后服务器返回一个响应,然后对响应数据进行分析,判断是否与我们预期的返回一致,从而验证功能是否正确。

二、HTTP协议解读

1)http协议:超文本传输协议

2)https:简单的来说,就是http的安全版,在http下加入了SSL层(SSL主要用户web的安全传输协议)

3)http的默认端口号是:80 ,默认的端口在url可以不加

   https的默认端口号是:443 ,默认的端口在url可以不加

4)HTTP请求过程

客户端:PC端的应用程序 浏览器 APP 小程序

HTTP通信:客户端发送给服务器的请求信息

       服务器返回给客户端的响应信息

客户端:前端----->主动请求。能够发起对应的请求的客户端。

服务端:后端----->被动接受。

  

  扩展URL:

5)HTTP请求信息

请求行: 请求方法/请求网址/协议版本
请求头部:header
host
connection
upgrade-insecure-requests
user-agent:用户代理,通过客户端代理
referer
accept-encoding
cookie
备注:域名和IP地址之间是映射关系,域名是为了好记

请求数据:

6)HTTP响应信息

状态行:状态码
消息报头:
    content-type:返回的数据格式
    test/html
    application/json
    application/xml

响应正文:

7)HTTP响应状态码

状态码           含义                 客户端client                                 服务器端server 
1xx      | Informational 信息     啥都不用做,知道就好                          信息收到了,后续会处理                               
2xx      | Successful 成功        啥都不用做,知道就好                          请求已正确处理                                               
3xx      | Redirection 重定向     重新请求返回的新地址                          client需要的内容,由于一些原因,比如地址已发生变化了,然后返回该内容的新地址 
4xx      | 客户端的错误            确保用正确的参数和信息正确,重新请求             请求已正确处理                               
5xx      | 服务器端的错误           都无需操作,服务器端改了bug后,重新发送请求      服务器端的代码的bug导致了出错

8)HTTP请求方法

get和post的区别:

a)应用场景不同

  get获取资源

  post提交数据,创建新的数据/对已有数据的修改

b)参数存放 

  get请求的参数都可以显示在浏览器网址上,通过?param=value【即query string方式】查询字符串

  post可以使用query string,但是通常不这么做,通常放到body请求体当中

c)安全性

  get和post并没有谁更安全,抓包都是可以看到里面的数据,网上说的post更安全是因为数据放到了body当中,肉眼看不到而已, 但其实也是不安全的,而get请求是直接在URL中肉眼可以看到

备注:加密与请求方法没关系,什么东西都是可以加密的

【WebService协议】

http 和 webservice 都是基于TCP/IP协议的应用层协议

webservice是基于http的soap协议传输数据 webservice=soap=http+xml,webservice协议就是有http+xml组成的,其中xml中会用到wsdl,wsdl是描述语言xml中的一种格式。

socket是基于TCP/IP的传输协议,是对TCP/IP协议的封装

socket和TCP都是基于TCP/IP传输层协议

注:Restful是一种接口规范,而不是接口协议,restful接口规范中也会用到http协议。

 因现在大部分会用http协议不用webservice协议,故没有实际操作,只是转载来了解理论。

一. WSDL WebService的创建:
1.创建【Web Service Project】:

image.png

WebServices Framework要选JAX-WS:

image.png

2.写一个简单的测试用例:

package com.webservice;

public class WebService{

public String printData(String printerName){
    String strRet = "Welcome to use WebService, " + printerName;
    
    System.out.println("Print from WebService:" + strRet);
    
    return strRet;
}   

}

3.发布Web Service:
点击工具栏的New Web Service:

image.png

Strategy选择第二个(Create web service from Java class):

image.png

勾选【Generate WSDL in project】:

image.png

点击【Finish】后,系统会在WEB-INF/wsdl下生成两个文件:

image.png

WebServiceService.wsdl:这个文件是用来描述Web Service内容的
<?xml version="1.0" encoding="UTF-8"?>

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.com/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WebServiceService" targetNamespace="http://webservice.com/">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice.com/" schemaLocation="WebServiceService_schema1.xsd"/>
</xsd:schema>
</types>
<message name="printData">
<part element="tns:printData" name="parameters"/>
</message>
<message name="printDataResponse">
<part element="tns:printDataResponse" name="parameters"/>
</message>
<portType name="WebServiceDelegate">
<operation name="printData">
<input message="tns:printData"/>
<output message="tns:printDataResponse"/>
</operation>
</portType>
<binding name="WebServicePortBinding" type="tns:WebServiceDelegate">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="printData">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="WebServiceService">
<port binding="tns:WebServicePortBinding" name="WebServicePort">
<soap:address location="http://localhost:8080/WebService/WebServicePort"/>
</port>
</service>
</definitions>

WebServiceService_schema1.xsd:用来说明Web Service的命令及其参数
比如:sample里面的WebService是【printData】,有一个String类型的参数【arg0】,返回值一个String类型的值。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.com/" targetNamespace="http://webservice.com/" version="1.0">

<xs:element name="printData" type="tns:printData"/>

<xs:element name="printDataResponse" type="tns:printDataResponse"/>

<xs:complexType name="printData">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="printDataResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

将WebService项目部署到Tomcat即可。
(部署方法略)

二. WSDL WebService的调用:
方法1:创建Web Service Client来调用:
1.创建【Java Project】:

image.png

2.点击工具栏的New Web Service Client:

image.png

image.png

3.选择【WSDL URL】:

image.png

4.点击【Next】完成创建后,在src/com/webservice下,自动生成相关文件。(WebServiceTest.java除外,这个是自己创建的调用文件)

image.png

5.创建【WebServiceTest.java】

image.png

代码如下:
package com.webservice;

public class WebServiceTest{

public static void main(String[] args){
    WebServiceService wssPrintData = new WebServiceService();
    WebServiceDelegate wsdPrintData = wssPrintData.getWebServicePort();
    
    System.out.println(wsdPrintData.printData("sun"));
}   

}

6.【WebServiceTest.java】右键→Run As→Java Application
输出结果:
Welcome to use WebService, sun

方法2:用HttpClient调用:
package com.httpclientforwsdl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

public class WebServiceHttpClientTest{

public synchronized static String accessService(String wsdl,String ns,String method,Map<String,String> params,String result)throws Exception{  
    //拼接参数  
    String param = getParam(params);  
    String soapResponseData = "";  
    //拼接SOAP  
    StringBuffer soapRequestData = new StringBuffer("");  
    soapRequestData.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");  
    soapRequestData.append("<soap:Body>");  
    soapRequestData.append("<ns1:"+method+" xmlns:ns1=\""+ns+"\">");  
    soapRequestData.append(param);  
    soapRequestData.append("</ns1:"+method+">");  
    soapRequestData.append("</soap:Body>" + "</soap:Envelope>");  
    PostMethod postMethod = new PostMethod(wsdl);  
    // 然后把Soap请求数据添加到PostMethod中  
    byte[] b=null;  
    InputStream is=null;  
    try {  
        b = soapRequestData.toString().getBytes("utf-8");   
        is = new ByteArrayInputStream(b, 0, b.length);  
        RequestEntity re = new InputStreamRequestEntity(is, b.length,"text/xml; charset=UTF-8");  
        postMethod.setRequestEntity(re);  
        HttpClient httpClient = new HttpClient();  
        int status = httpClient.executeMethod(postMethod);  
        System.out.println("status:"+status);  
        if(status==200){  
            soapResponseData = getMesage(postMethod.getResponseBodyAsString(),result);  
        }  
    } catch (Exception e) {  
        e.printStackTrace();  
    } finally{  
        if(is!=null){  
            is.close();  
        }  
    }  
    return soapResponseData;  
}  
  
public static String getParam(Map<String,String> params){  
    String param = "";  
    if(params!=null){  
        Iterator<String> it  = params.keySet().iterator();  
        while(it.hasNext()){  
            String str = it.next();  
            param+="<"+str+">";  
            param+=params.get(str);  
            param+="</"+str+">";  
        }  
    }  
    return param;  
}  
  
public static String getMesage(String soapAttachment,String result){  
    System.out.println("message:"+soapAttachment);  
    if(result==null){  
        return null;  
    }  
    if(soapAttachment!=null && soapAttachment.length()>0){  
        int begin = soapAttachment.indexOf(result);  
        begin = soapAttachment.indexOf(">", begin);  
        int end = soapAttachment.indexOf("</"+result+">");  
        String str = soapAttachment.substring(begin+1, end);  
        str = str.replaceAll("<", "<");  
        str = str.replaceAll(">", ">");  
        return str;  
    }else{  
        return "";  
    }  
}  
  
/** 
 * @param args 
 */  
public static void main(String[] args) {   
    try {  
        Map<String,String> param = new HashMap<String,String>();  
        param.put("arg0", "sun");
        String wsdl="http://localhost:8080/WebService/WebServicePort?wsdl";  
        String ns = "http://webservice.com/";  
        String method="printData";  
        String response =accessService(wsdl,ns,method,param,"return");  
        System.out.println("main:"+response);  
          
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}  

}

显示结果:
status:200
七月 15, 2016 3:43:27 下午 org.apache.commons.httpclient.HttpMethodBase getResponseBody
警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
message:<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:printDataResponse xmlns:ns2="http://webservice.com/"><return>Welcome to use WebService, sun</return></ns2:printDataResponse></S:Body></S:Envelope>
main:Welcome to use WebService, sun

【Dubbo 接口测试方法]

一.直接通telnet然后用dubbo协议调用方法

(1)在项目的配置文件中可以看到

dubbo.protocol.port=10022

说明dubbo对外暴漏的端口为10022,直接用telnet访问此端口。

telnet lcoalhost 10022然后就能看到

说明连接成功。

  1. 用ls查看服务

  1. 查看服务下有那些方法ls -l

  1. 然后使用invoke测试接口,这里注意,pom需要添加 fastjson的依赖不然报Invalid json argument, cause: com/alibaba/fastjson/JSON dubbo

二.Idea 插件dubbo Invoke

在idea插件超市中发现dubbo invoke 集成了invoke.

  1. 下载安装插件DubboInvoke

  1. 在对外暴漏的方法上加注释

/**
 *
 * @param name 姓名
 *             example=haha
 * @return
 */
String sayHello(String name);

  1. 调用,点击这个接口出现的地方

选择使用插件,这里注意serverAddress为dubbo的服务ip 端口。

然后点击invoke,就能看到代测试结果。

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值