Webservice interface and HTTP interface study notes 1. The concept of webservice
Table of Contents: Introduction
1. The concept of webservice
The Web is a technology that enables applications to communicate with each other in a platform- and programming language-independent manner. A web service is a software interface that describes a set of operations that can be accessed over the network through standardized XML messaging. It uses an XML language-based protocol to describe an operation to be performed or data to be exchanged with another Web service. A set of Web services that interact in this way defines a special Web services application in a Service-Oriented Architecture (SOA).
Simply put, WebService is an SOA (service-oriented programming) architecture. It does not depend on language or platform. It can realize mutual calls between different languages (described through xml) and perform HTTP protocol-based services through the Internet. Interaction between network applications. Software services provided over the Web via SOAP, described using WSDL files, and registered via UDDI.
XML: (Extensible Markup Language) Extensible Markup Language. For short-term temporary data processing and the World Wide Web, it is the foundation of Soap.
Soap: (Simple Object Access Protocol) Simple Object Access Protocol. It is the communication protocol of XML Web Service. When the user finds your WSDL description document through UDDI, he can call one or more operations in the Web service you created through SOAP. SOAP is a specification for calling methods in the form of XML documents. It can support different underlying interfaces, such as HTTP(S) or SMTP.
WSDL: (Web Services Description Language) A WSDL file is an XML document that describes a set of SOAP messages and how to exchange them. In most cases automatically generated and used by software.
UDDI (Universal Description, Discovery, and Integration) is a new project mainly aimed at Web service providers and users. Before users can call a Web service, they must determine which business methods are included in the service, find the called interface definition, and compile software on the server side. UDDI is a mechanism that guides the system to find the corresponding service based on the description document. UDDI uses SOAP messaging mechanism (standard XML/HTTP) to publish, edit, browse and find registration information. It uses XML format to encapsulate various types of data, and sends it to the registration center or the registration center returns the required data.
2. Webservice interface test
To test the webservice interface, you can use soapUI to test. Let's simply use soapUI to call the webservice interface:
1. Open soapUI and create a new soap project:
2. Let’s take the wsdl address of weather forecast as an example and import soapUI to view all interface information in it;
wsdl address http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
3. View the interface and messages, enter parameters and call directly
Send message:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">
<soapenv:Header/>
<soapenv:Body>
<web:getSupportCity>
<!--Optional:-->
<web:byProvinceName>北京</web:byProvinceName>
</web:getSupportCity>
</soapenv:Body>
</soapenv:Envelope>
Return message:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getSupportCityResponse xmlns="http://WebXml.com.cn/">
<getSupportCityResult>
<string>北京 (54511)</string>
<string>上海 (58367)</string>
<string>天津 (54517)</string>
<string>重庆 (57516)</string>
</getSupportCityResult>
</getSupportCityResponse>
</soap:Body>
</soap:Envelope>
1. Root element: Envelope.
2. Header element:: It is not forced to appear and is controlled by the programmer. It is mainly used to carry some additional information, such as user name and password.
3. Body: The call is correct. The content of the body element should comply with the format required by WSDL.
3. HTTP interface
Http protocol is based on TCP protocol. When the browser needs to obtain web page data from the server, it will issue an Http request. Http will establish a connection channel to the server through TCP. When the data required for this request is completed, Http will immediately disconnect the TCP connection. This process is very short. Therefore, the HTTP connection is a short connection and a stateless connection.
The main features of the HTTP protocol can be summarized as follows:
1. Support client/server mode.
2. Simple and fast: When the client requests services from the server, it only needs to transmit the request method and path. Commonly used request methods are GET, HEAD, and POST. Each method specifies a different type of contact between the client and the server. Due to the simplicity of the HTTP protocol, the program size of the HTTP server is small and the communication speed is very fast.
3. Flexible: HTTP allows the transmission of any type of data object. The type being transferred is marked by Content-Type.
4. No connection: The meaning of no connection is to limit each connection to only process one request. After the server processes the client's request and receives the client's response, it disconnects. This method saves transmission time.
5. Stateless: HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory ability for transaction processing. The lack of status means that if subsequent processing requires the previous information, it must be retransmitted, which may result in an increase in the amount of data transferred per connection. On the other hand, the server responds faster when it does not need previous information.
URL parsing
On the WWW, each information resource has a unified and unique address on the Internet. This address is called a URL (Uniform Resource Locator, Uniform Resource Locator). It is the Uniform Resource Locator of the WWW, which refers to the network address. The HTTP protocol works on a client-server architecture. As an HTTP client, the browser sends all requests to the HTTP server, that is, the WEB server, through the URL. The web server sends response information to the client based on the received request.
The URL consists of three parts: resource type, host domain name where the resource is stored, and resource file name.
The general syntax format of URL is (square brackets [] are optional):
protocol :// hostname[:port] / path / [;parameters][?query]#fragment
Take the following URL as an example:
http://blog.sina.com.cn/s/blog_537ad6610102xtb1.html?tj=hist
1. The protocol part means that the page uses the http protocol. A variety of protocols can be used on the Internet, such as HTTP, FTP, etc. The "//" after "HTTP" is the delimiter;
2. For the domain name part, blog.sina.com.cn, you can also use the IP address as the domain name, such as: 192.168.55.14:8080, where 8080 is the port, and ":" is used as the separator between the domain name and the port. The port is not a required part of a URL. If the port part is omitted, the default port 80/tcp will be used;
3. The virtual directory part, starting from the first "/" after the domain name to the last "/", is the virtual directory part. The virtual directory is also not a required part of a URL. The virtual directory in this example is "/s/"
4. File name part: It starts from the last "/" after the domain name and ends with "?". It is the file name part. If there is no "?", it starts from the last "/" after the domain name and ends with "#". , is the file part. If there are no "?" and "#", then from the last "/" after the domain name to the end, it is the file name part. The file name in this example is "blog_537ad6610102xtb1.html". The file name part is not a required part of a URL. If this part is omitted, the default file name is used.
5. Anchor part: From the beginning to the end of "#", it is the anchor part. The anchor part is not a necessary part of a URL (can be understood as positioning)
6. Parameter part: The part starting from "?" to "#" is the parameter part, also known as the search part and the query part. The parameter part in this example is "7. Parameter part: from "? The part between "" and "#" is the parameter part, also called the search part and the query part. In this example, the parameter part is "boardID=5&ID=24618&page=1". Multiple parameters can be allowed Parameters, use "&" as the separator between parameters.". Parameters can allow multiple parameters, and "&" is used as a separator between parameters.
4. HTTP protocol structure
HTTP request message
The client sends an HTTP request to the server. The request message includes the following format:
It consists of four parts: request line, request header, blank line and request data.
The format is as follows:
<request-line>
<headers>
<blank line>
[<request-body>
1. Request line
The request line consists of three fields: the request method field, the URL field, and the HTTP protocol version field, which are separated by spaces. For example, GET /index.html HTTP/1.1.
The request methods of HTTP protocol include GET, POST, HEAD, PUT, DELETE, OPTIONS, TRACE, and CONNECT.
The most commonly used methods are:
GET method: When accessing a webpage by entering the URL in the address bar of the browser, the browser uses the GET method to obtain the data from the server. Resources,
The POST method requires the requested server to accept the data attached to the request, and is often used to submit forms.
2. Request header
The request header consists of keyword/value pairs, one pair per line, and the keywords and values are separated by English colon ":". The request header informs the server about the client's request. Typical request headers are:
User-Agent: The browser type that generated the request.
Accept: List of content types recognized by the client.
Host: The requested host name, allowing multiple domain names to be at the same IP address, that is, a virtual host.
3. Blank line
The last request header is followed by a blank line, carriage return and line feed characters are sent to notify the server that there are no more request headers to follow.
4. Request text
The request data is not used in the GET method, but in the POST method. The POST method is suitable for situations where customers are required to fill out a form. The most commonly used request headers related to request data are Content-Type and Content-Length.
HTTP response message
After receiving and interpreting the request message, the server returns an HTTP response message.
The HTTP response also consists of three parts: status line, message header, and response body.
Message format:
<status-line>
<headers>
<blank line>
[<response-body>]
1. The status line format is as follows:
HTTP-Version Status-Code Reason-Phrase CRLF
Among them, HTTP-Version represents the version of the server HTTP protocol; Status-Code represents the response status code sent back by the server; Reason-Phrase represents the text description of the status code.
The status code consists of three digits. The first digit defines the type of response and has five possible values:
1xx: Indication information--indicates that the request has been received, continue processing
2xx: Success--indicates that the request has been successfully received, understood, and accepted
3xx: Redirect--further operation is necessary to complete the request
4xx: Client error--the request has a syntax error or the request cannot be fulfilled
5xx : Server-side error--the server failed to implement a legal request
Common status codes, status descriptions, instructions:
200 OK //Client request successful a> eg: HTTP/1.1 200 OK (CRLF) 503 Server Unavailable //The server is currently unable to process the client's request and may recover after a period of time Normal 500 Internal Server Error //An unexpected error occurred in the server 404 Not Found //The requested resource does not exist, eg: an error was entered URL 403 Forbidden //The server received the request, but refused to provide the service 401 Unauthorized //The request is unauthorized, this status code must be the same as WWW-Authenticate Use header fields together
400 Bad Request //The client request has a syntax error and cannot be understood by the server
2. Response header
The response header is used to describe the basic information of the server and the description of the data. The server can use the description information of these data to inform the client how to process the data it will send back after a while.
3. Response text
The response body is the message body of the response. If it is pure data, pure data will be returned. If the request is an HTML page, then the HTML code will be returned. If it is JS, it will be JS code.
The difference between post and get
Get request: Get the request from the server; the data is placed in the URL and has a length limit;
Post request: Send a request to the server; the data is placed in the body, with no length limit; more secure
The http interface returns a json string: a common data type
The return value of the interface;
{
"error_code": 0,
"stu_info": [
{
"id": 1,
"name": "灏忛粦",
"sex": 男",
"age": 18,
"addr": "北京市海淀区",
"grade": "三年二班",
"phone": "18612532941",
"gold": 11212
}
]
}
Test interface call drill
get interface
请求URL http://api.nnzhp.cn/api/user/stu_info
- Request method get
- Request parameters
Parameter name Required Type Description
stu_name is string student name
1. Call directly from the browser
2. Call using postman
You can see that when postman selects the get method, the body is disabled, and the http interface request of the get method generally puts the data in the URL.
The return method is also in the form of json string;
3. Use jmeter to test the get method call
Create thread group
Add HTTP request
Enter domain name, URL, select request method, and add key
Add view results tree
execution interface
You can view the execution results and return data in the result tree
post request interface
Login interface
请求URL
http://api.nnzhp.cn/api/user/login
- Request method post
- Request parameters Parameters are in key-value form
Parameter name Required Type Description
username is string username
passwd is string password
登陆用户名:niuhanyang
密码:aA123456
Return parameter description
Parameter name Type Description
Error_code int error encoding, 0 is successful operation
Login_info object Login object information
userId int userid
Signature string Signature
Login_time string Login time
1. Call postman
The parameters of the post method are placed in the body. You can see that it is invalid to put the parameters in the URL.
Put the parameters in the body before submitting
Put it in the body, pass the parameters in the form of key-value, and return the json string. Refer to the return parameter description to see that the login is successful.
2. Use jmeter to perform call testing
Open jmeter ->Add->threads->Thread Group
Add->sampler->HTTP request in the thread group
Enter the domain name, URL, select the method as post, and enter the parameters
Execute and view the result return value
Add student information interface
请求URL:http://api.nnzhp.cn/api/user/add_stu
- Request method post
- Parameter, the input parameter is json type
Parameter name optional type description
name is string student name
grade is string class
phone is string phone
“
Age No string Age, if not passed, the default is 18
No string Address, if not passed, the default is Changping District, Beijing
Return parameter description
Parameter name Type Description
.
msg string prompt message
As described above, if the input parameter format is json, then it is not possible to pass parameters in the key-value format, as follows:
Now we use the correct parameter input method to make the call
1. Postman call
In postman, select the method as post, enter the URL address, select raw in the body, and enter the parameters in json string format
2. Use jmeter to call the interface
Let’s take a look at how jmeter makes calls
Create an http request, enter the domain name, path, method as post, enter json in the body data, and then execute
You can view the content of the request message in the request item in the result tree.
Check the response result in the response data and the execution is successful.
Student gold coin recharge
请求URL:
http://api.nnzhp.cn/api/user/gold_add
- Request method post
- parameters ,
This interface has permission verification. Only the admin user can perform operations. Cookies need to be added.
The key in the cookie is the logged in user name, the value is obtained from the login interface, and sign will be returned after successful login.
Parameter name optional type description
Stu_id is an int student ID
gold is the int number of gold coins
Return parameter description
Parameter name Type Description
Error_code int error encoding, 0 is successful operation
msg string prompt message
As described above, the input parameter form needs to add cookies
1. How to call postman and add cookies
To put it simply, a cookie is a key-value pair stored in the user's browser.
When it comes to cookies, the session cannot be missing. What is a session? A session is a key-value pair saved on the server. Generally, the session is stored in the cache because it is used frequently. If you want to improve performance, put the session directly into the memory. .
First, we view the student's ID information through the "Get Student Information" interface, and then obtain the signature value through the "Login" interface as follows: Take the test user as an example:
Query ID=2341
The sign value is: cd2b43f1688e472e3a516b5a2c6831e8
Now add gold coins for students
2. Use jmeter to call the interface
Using jmeter to test http requests with cookies requires adding an http cookie manager
Let’s first look at the situation where cookies are added and executed.
You can see that when cookies are not added, the execution fails.
Now we add the httpcookie manager, right-click to add->Configuration Component->HTTP cookie manager
Add cookie value
Execute and view the requested data
Get all student information
请求URL:
http://api.nnzhp.cn/api/user/all_stu
- Request method get
- parameter, needs to add header information, the key is Referer and the value is http://api.nnzhp.cn/
Return parameter description
Parameter name Type Description
Error_code int error encoding, 0 is successful operation
All_stu list prompt information
1. Let’s take a look at the result of accessing directly using the browser address bar without addingheader
As shown below, if no header is added for access, it will prompt that the origin is incorrect.
2. Use postman to call, add header information, and call
Executed, the result displays all student user information
3. Use jmeter to perform call testing
Add http request
Right click to add->Configure component->http header manager
The execution result is successful and student information is displayed.
File upload type
请求URL:
http://api.nnzhp.cn/api/file/file_upload
- Request method post
- parameter
Parameter name optional type description
file is file file
Return parameter description
Parameter name Type Description
Error_code int error encoding, 0 is successful operation
msg string prompt message
1. Use postman to call, enter the URL address, and select the file type as the key type.
Select file to upload
Operation performed successfully
2. Use jmeter to call
Executed, operation successful
write at the end
If you think the article is good, please like, share, and leave a message because this will allow me to continue to output more high-quality The strongest motivation for articles!
If anyone who reads this article feels that my understanding is wrong, comments and discussions are welcome ~
You can also join the group below to communicate and learn from fellow experts.