curl usage guidelines

 

First, view the page source code

Directly after the curl command with the URL, you can see the page source. Our website www.sina.com for example (select the Web site, mainly because of its short page code):

  $ curl www.sina.com

              

Moved Permanently

  

The document has moved here.

  

If you want to survive this page, you can use -oparameters, which is equivalent to using wget commands.

  $ Curl -o [filename] www.sina.com

Second, automatically jump

Some URLs are automatically jump in. Use -Lparameters, curl will jump to the new URL.

  $ curl -L www.sina.com

Type the above command, the result automatically jump to www.sina.com.cn .

Third, the display header information

-iParameters can display the header http response, along with the web page code.

  $ curl -i www.sina.com

  HTTP/1.0 301 Moved Permanently   Date: Sat, 03 Sep 2011 23:44:10 GMT   Server: Apache/2.0.54 (Unix)   Location: http://www.sina.com.cn/   Cache-Control: max-age=3600   Expires: Sun, 04 Sep 2011 00:44:10 GMT   Vary: Accept-Encoding   Content-Length: 231   Content-Type: text/html; charset=iso-8859-1   Age: 3239   X-Cache: HIT from sh201-9.sina.com.cn   Connection: close

              

Moved Permanently

  

The document has moved here.

  

-IParameter is displayed only the header information http response.

Fourth, the display communication process

-vParameter may be displayed to the entire process http communication, comprising port and http request header information.

  $ Curl -v www.sina.com

  • About to connect() to www.sina.com port 80 (#0)

  • Trying 61.172.201.195... connected

  • Connected to www.sina.com (61.172.201.195) port 80 (#0)

GET / HTTP/1.1

User-Agent: curl/7.21.3 (i686-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18

Host: www.sina.com

Accept: /

  • HTTP 1.0, assume close after body < HTTP/1.0 301 Moved Permanently   < Date: Sun, 04 Sep 2011 00:42:39 GMT   < Server: Apache/2.0.54 (Unix)   < Location: http://www.sina.com.cn/   < Cache-Control: max-age=3600   < Expires: Sun, 04 Sep 2011 01:42:39 GMT   < Vary: Accept-Encoding   < Content-Length: 231   < Content-Type: text/html; charset=iso-8859-1   < X-Cache: MISS from sh201-19.sina.com.cn   < Connection: close   <

              

Moved Permanently

  

The document has moved here.

     * Closing connection #0

If you think the above information is not enough, then the following command to view more detailed communication process.

  $ curl --trace output.txt www.sina.com

or

  $ curl --trace-ascii output.txt www.sina.com

After running, open output.txt file viewing.

Fifth, sending form information

Send form Offers GET and POST methods. GET method is relatively simple, as long as the data is appended to the URL line.

  $ curl example.com/form.cgi?data=xxx

POST method must separate the data and URL, curl necessary to use --data parameters.

  $ curl -X POST --data "data=xxx" example.com/form.cgi

If your data has not been encoded form, but also allows you to curl encoding parameters --data-urlencode.

  $ curl -X POST--data-urlencode "date=April 1" example.com/form.cgi

Six, HTTP verb

curl default HTTP verb is GET, using -Xparameters may support other verbs.

  $ curl -X POST www.example.com

  $ curl -X DELETE www.example.com

Seven, file upload

Assuming that the file upload form is this:

  
             

So you can upload files with a curl:

  $ curl --form upload=@localfilename --form press=OK [URL]

Eight, Referer field

Sometimes you need to http request header information, a referer field represents a jump from where you came.

  $ curl --referer http://www.example.com http://www.example.com

Nine, User Agent field

This field is used to indicate the device information of the client. Sometimes the server according to this field, for different devices, return to the page in different formats, such as mobile phones and desktop versions.

iPhone4 is the User Agent

  Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7

curl can simulate this:

  $ curl --user-agent "[User Agent]" [URL]

Ten, cookie

Use --cookieparameters help the curl send cookie.

  $ curl --cookie "name=xxx" www.example.com

As for the specific value of the cookie, http response from the header information Set-Cookieobtained in the field.

-c cookie-fileCan be saved to a file server returns a cookie, -b cookie-fileyou can use this information as a cookie file, follow-up requests.

  $ curl -c cookies http://example.com   $ curl -b cookies http://example.com

Eleven, increased header information

Sometimes necessary in the http request, add a header itself. --headerParameters can play this role.

  $ curl --header "Content-Type:application/json" http://example.com

Twelve, HTTP Authentication

Some domain HTTP authentication is required, then curl need to use --userparameters.

  $ curl --user name:password example.com

XIII, crawling pictures

-The curl http://aliimg.changba.com/cache/photo/ [260,929,610 to 260,939,610] _640_640.jpg

 

Fourth, crawling web pages and download the source code

Open a command line, type the following command, the server may send an acquisition request to the home Baidu source code> command to save the baidu.html source is output to the current directory.

curl https://www.baidu.com > baidu.html

Fifth, get a form · GET or POST method

Webpage <form> tag used to generate a form. When we click the submit button in a form, the browser will fill in all the forms speak parameter analysis and packaging, and ultimately sent to the destination network address with the specified HTTP. After the code is shown below the box user input parameter information is added to fill the URL, and transmits the GET method judge.php the target server. GET methods, and the respective values correspond fields within the form.

 

GET method parameters will appear in the URL address, therefore, we are also in simulation-based command line cURL this.

curl www.example.com/judge.php?year=1997&press=ok

URL syntax specified, a generic URL address format, is easy to see, year code field on the embodiment and its value is <params> instance.

<scheme>://<user>:<password>@<host>:<port>/<path>:<params>?<query>#<frag>

The difference is that with the GET method, HTTP POST beg transfer of data to the server, because the private information such as account passwords often, these data are often added to HTML Header, while not visible in the URL address, transfer data to the target server. CURL command line processing method of the POST method is -d parameter.

curl -d "year=1997&press=OK" www.example.com/judge.php

XVI upload files · POST or PUT method

The end of 1995, RFC 1867 defines a new POST method to upload files. Mainly used to upload local files to the server. The page is written like this:

CURL command with the corresponding command parameters as follows -F.

curl -F upload=@localfilename -F press=OK URL

HTTP protocol standard method is to use file upload PUT, this time using the -T command cURL parameters:

curl -T uploadfile www.uploadhttp. com/receive.php

XVII disguised as specified client

Some network resources first need to determine what the user's browser is standards-compliant and we have to be able to browse or download. At this point you can curl himself "disguised" as any other browser:

curl -A "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" URL

This directive represents cURL disguised IE5.0, the user platform is Windows 2000. curl -A "Mozilla / 4.73 [en ] (X11; U; Linux 2.2.15 i686)" URL

At this point cURL became Netscape, running on a Linux platform PIII.

 

 

about me

Private blog

Public micro-channel number: infree6 either directly scan code

 

Guess you like

Origin www.cnblogs.com/songjianzaina/p/11303709.html