An HTTP GET and post
The difference between get and post: Getting is fetching data from the server, and post is transferring data to the server.
(1) parameter transmission mode,
Get commits, and the requested data is appended to the URL to split the URL and transmit the data, with multiple parameters & connections; For example: Login.action?name=hyddd&password=idontknow&verify=%e4 %bd%a0%E5%A5%BD. If the data is an English letter/number, sent as is, if it is a space, converted to +, if it is Chinese/other characters, the string is directly encrypted with Base64, such as:%E4%BD%A0%E5%A5%BD, where the xx in%xx for the symbol in the 16 in ASCII representation.
Post submission: The submitted data is placed in the package body of the HTTP package. As a result, the data submitted by get is displayed in the Address bar, and post submission will not change the address bar.
(2) The size of the transmitted data,
First, the HTTP protocol does not limit the size of the data being transmitted, nor does the HTTP protocol specification limit the length of the URL. And in the actual development of the main limitations are:
Get: Specific browsers and servers have restrictions on the length of URLs, such as IE's limit of 2083 bytes (2k+35) for URL lengths. For other browsers, such as Netscape, Firefox, etc., there is no theoretical length limit, the limit depends on the operating system support. So for get commits, the transfer data is limited by the length of the URL.
Post: Theoretically, the data is not restricted because it is not passed through a URL. However, the actual Web server will specify the size of the post submission data limits, Apache, IIS6 have their own configuration.
(3) Security:
Post is more secure than get. The meaning of security here is the meaning of the real one, for example: Submit data through GET, username and password will be clear in the URL, because (1) the login page may be cached by the browser, (2) Other people to view the history of the browser, then someone else can get your account and password, In addition, submitting data using Get can also cause Cross-site request forgery attacks.
Get and Post are the same: the Get,post protocol is run on HTTP.
Get: Request parameter is the length of the query string attached to the URL as a key/value pair (query string) is limited by the Web browser and Web server (ie supports up to 2048 characters) and is not suitable for transmission of large datasets while it is unsafe.
Post: The request parameter is transmitted in a different part of the HTTP header (named entity body), which is used to transfer form information, so the Content-type must be set to: application/x-www-form-urlencoded. Post design is used to support user fields on Web Forms, and their parameters are also transferred as Key/value. However: it does not support complex data types because post does not define the semantics and rules of the transport data structure.
Simply put, HTTP Web services refer to the direct use of HTTP operations to send and receive data from a remote server in a programmatic way. If you want to retrieve data from the server, use HTTP GET, and if you want to send new data to the server, use the HTTP post. Some of the more advanced HTTP Web service APIs also allow the use of HTTP put and HTTP Delete to create, modify, and delete data.
Two Web instances
Examples from: http://fy.webxml.com.cn/webservices/englishchinese.asmx?op=translatorstring, a free webservice for translating in English and Chinese, Also provides GET+POST+SOAP access support.
Three Python HTTP web libraries
Python 3 comes with two libraries for interacting with HTTP Web services:
Http.client is the underlying library of the HTTP protocol.
Urllib.request is built on an abstraction layer above http.client. It provides a standard API for accessing HTTP and FTP servers, can automatically follow HTTP redirection, and handles some common forms of HTTP authentication.
Httplib2, a third-party open Source Library, implements the HTTP protocol more fully than Http.client, while providing a better abstraction than urllib.request.
Python's HTTP library does not support caching, but HTTPLIB2 support.
The Python HTTP library does not support the last modification time check, but httplib2 support.
The Python HTTP library does not support ETag and HTTPLIB2 support.
Python's HTTP library does not support compression, but httplib2 support.
Httplib2 to help you with the permanent redirect. Not only will it tell you that a permanent redirect has occurred, but it will log these redirects locally and automatically overwrite the redirected URL before sending the request.
HTTPLIB2 's
Download: http://code.google.com/p/httplib2/
Installation: python31 setup.py Install
Four httplib2 using GET and post instances
#-*-Coding:utf-8-*-
Def testhttpget ():
Import Httplib2
#httplib2. DebugLevel = 1
Word= ' China '
Urlstr = ' http://fy.webxml.com.cn/webservices/englishchinese.asmx/translatorstring ' + '? wordkey= ' + Word
h = httplib2.http ('. Cache ')
Response,content = H.request (URLSTR)
#for item in Response.items (): Print (item)
Print (Content.decode (' Utf-8 '))
#print (content)
Def testhttppost ():
Import Httplib2
From Urllib.parse import UrlEncode
#httplib2. DebugLevel = 1
Word= ' America '
Urlstr = ' http://fy.webxml.com.cn/webservices/englishchinese.asmx/translatorstring '
data={' Wordkey ': Word}
h = httplib2.http ('. Cache ')
Response,content = H.request (Urlstr, ' Post ', UrlEncode (data), headers={' content-type ': ' application/ X-www-form-urlencoded '})
#for item in Response.items (): Print (item)
Print (Content.decode (' Utf-8 '))
#print (content)
Testhttpget ()
Testhttppost ()
The results are as follows:
When translated from English to Chinese, the resulting XML appears to be UTF8, always with errors:
Content.decode (' utf-8 ') error unicodeencodeerror: ' GBK ' codec can ' t encode character ' u0283 ' in position 224:illegal E sequence