curl请求失败重复请求

curl is a a command line tool that allows to transfer data across the network.

curl是一个命令行工具,可用于通过网络传输数据。

It supports lots of protocols out of the box, including HTTP, HTTPS, FTP, FTPS, SFTP, IMAP, SMTP, POP3, and many more.

它支持许多现成的协议,包括HTTP,HTTPS,FTP,FTPS,SFTP,IMAP,SMTP,POP3等。

When it comes to debugging network requests, curl is one of the best tools you can find.

在调试网络请求时,curl是您可以找到的最佳工具之一。

It’s one of those tools that once you know how to use you always get back to. A programmer’s best friend.

它是您一旦知道如何使用的工具之一,便会不断得到使用。 程序员最好的朋友。

It’s universal, it runs on Linux, Mac, Windows. Refer to the official installation guide to install it on your system.

它具有通用性,可以在Linux,Mac,Windows上运行。 请参阅官方安装指南以将其安装在系统上。

Fun fact: the author and maintainer of curl, swedish, was awarded by the king of Sweden for the contributions that his work (curl and libcurl) did to the computing world.

有趣的事实:curl的作者和维护者,瑞典人,由于他的工作(curl和libcurl)对计算世界的贡献而被瑞典国王授予。

Let’s dive into some of the commands and operations that you are most likely to want to perform when working with HTTP requests.

让我们深入了解一些在处理HTTP请求时最可能要执行的命令和操作。

Those examples involve working with HTTP, the most popular protocol.

这些示例涉及使用最流行的协议HTTP。

  • Perform an HTTP GET request 执行HTTP GET请求
  • Get the HTTP response headers 获取HTTP响应头
  • Only get the HTTP response headers 仅获取HTTP响应标头
  • Perform an HTTP POST request 执行HTTP POST请求
  • Perform an HTTP POST request sending JSON 执行发送JSON的HTTP POST请求
  • Perform an HTTP PUT request 执行HTTP PUT请求
  • Follow a redirect 跟随重定向
  • Store the response to a file 将响应存储到文件
  • Using HTTP authentication 使用HTTP身份验证
  • Set a different User Agent 设置其他用户代理
  • Inspecting all the details of the request and the response 检查请求和响应的所有详细信息
  • Copying any browser network request to a curl command 将任何浏览器网络请求复制到curl命令

(Perform an HTTP GET request)

When you perform a request, curl will return the body of the response:

当您执行请求时,curl将返回响应的主体:

curl https://flaviocopes.com/

(Get the HTTP response headers)

By default the response headers are hidden in the output of curl. To show them, use the i option:

默认情况下,响应标头隐藏在curl的输出中。 要显示它们,请使用i选项:

curl -i https://flaviocopes.com/

(Only get the HTTP response headers)

Using the I option, you can get only the headers, and not the response body:

使用I的选项,你可以得到只有标题,而不是响应主体:

curl -I https://flaviocopes.com/

(Perform an HTTP POST request)

The X option lets you change the HTTP method used. By default, GET is used, and it’s the same as writing

X选项使您可以更改所使用的HTTP方法。 默认情况下,使用GET,与编写相同

curl -X GET https://flaviocopes.com/

Using -X POST will perform a POST request.

使用-X POST将执行POST请求。

You can perform a POST request passing data URL encoded:

您可以执行POST请求,传递经过编码的数据URL:

curl -d "option=value&something=anothervalue" -X POST https://flaviocopes.com/

In this case, the application/x-www-form-urlencoded Content-Type is sent.

在这种情况下,将发送application/x-www-form-urlencoded Content-Type。

(Perform an HTTP POST request sending JSON)

Instead of posting data URL-encoded, like in the example above, you might want to send JSON.

您可能想要发送JSON,而不是像上面的示例那样发布经过URL编码的数据。

In this case you need to explicitly set the Content-Type header, by using the H option:

在这种情况下,您需要使用H选项显式设置Content-Type标头:

curl -d '{"option": "value", "something": "anothervalue"}' -H "Content-Type: application/json" -X POST https://flaviocopes.com/

You can also send a JSON file from your disk:

您还可以从磁盘发送JSON文件:

curl -d "@my-file.json" -X POST https://flaviocopes.com/

(Perform an HTTP PUT request)

The concept is the same as for POST requests, just change the HTTP method using -X PUT

该概念与POST请求相同,只是使用-X PUT更改HTTP方法

(Follow a redirect)

A redirect response like 301, which specifies the Location response header, can be automatically followed by specifying the L option:

像301这样的重定向响应,它指定了Location响应头,然后可以通过指定L选项自动跟随它:

curl http://flaviocopes.com/

will not follow automatically to the HTTPS version which I set up to redirect to, but this will:

不会自动遵循我设置为重定向到的HTTPS版本,但这将:

curl -L http://flaviocopes.com/

(Store the response to a file)

Using the o option you can tell curl to save the response to a file:

使用o选项,您可以告诉curl将响应保存到文件:

curl -o file.html https://flaviocopes.com/

You can also just save a file by its name on the server, using the O option:

您还可以使用O选项将文件的名称保存在服务器上:

curl -O https://flaviocopes.com/index.html

(Using HTTP authentication)

If a resource requires Basic HTTP Authentication, you can use the u option to pass the user:password values:

如果资源需要基本HTTP身份验证,则可以使用u选项传递user:password值:

curl -u user:pass https://flaviocopes.com/

(Set a different User Agent)

The user agent tells the server which client is performing the request. By default curl sends the curl/<version> user agent, like: curl/7.54.0.

用户代理告诉服务器哪个客户端正在执行请求。 默认情况下,curl发送curl/<version>用户代理,例如: curl/7.54.0 。

You can specify a different user agent using the --user-agent option:

您可以使用--user-agent选项指定其他用户代理:

curl --user-agent "my-user-agent" https://flaviocopes.com

(Inspecting all the details of the request and the response)

Use the --verbose option to make curl output all the details of the request, and the response:

使用--verbose选项可使curl输出请求和响应的所有详细信息:

curl --verbose -I https://flaviocopes.com/

*   Trying 178.128.202.129...
* TCP_NODELAY set
* Connected to flaviocopes.com (178.128.202.129) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: flaviocopes.com
* Server certificate: Let's Encrypt Authority X3
* Server certificate: DST Root CA X3
> HEAD / HTTP/1.1
> Host: flaviocopes.com
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Cache-Control: public, max-age=0, must-revalidate
Cache-Control: public, max-age=0, must-revalidate
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8
< Date: Mon, 30 Jul 2018 08:08:41 GMT
Date: Mon, 30 Jul 2018 08:08:41 GMT
...

(Copying any browser network request to a curl command)

When inspecting any network request using the Chrome Developer Tools, you have the option to copy that request to a curl request:

使用Chrome开发者工具检查任何网络请求时,您可以选择将该请求复制到curl请求:

RxJava 重复请求_网络

curl 'https://github.com/curl/curl' -H 'Connection: keep-alive' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' -H 'Upgrade-Insecure-Requests: 1' -H 'DNT: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: https://www.google.it/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9,it;q=0.8' -H 'Cookie: _octo=GH1.1.933116459.1507545550; _ga=GA1.2.643383860.1507545550; tz=Europe%2FRome; user_session=XXXXX; __Host-user_session_same_site=YYYYYY; dotcom_user=flaviocopes; logged_in=yes; has_recent_activity=1; _gh_sess=ZZZZZZ' --compressed

翻译自: https://flaviocopes.com/http-curl/

curl请求失败重复请求