通过curl获取网页当前的http状态码
操作实例如下:
bbq@DESKTOP-SOR0NVK MINGW64 /d/gits/plan9 (main)$ curl -o /dev/null -s -w "%{http_code}" https://plan9.cn200
说明:
-o输出,定向到/dev/null-s静默模式,不显示过程-w格式化输出,相关说明可通过curl --help -w来查看
bbq@DESKTOP-SOR0NVK MINGW64 /d/gits/plan9 (main)$ curl --help -w-w, --write-out <format>Make curl display information on stdout after a completedtransfer. The format is a string that may contain plain text mixedwith any number of variables. The format can be specified as aliteral "string", or you can have curl read the format from a filewith "@filename" and to tell curl to read the format from stdinyou write "@-".The variables present in the output format are substituted by thevalue or text that curl thinks fit, as described below. Allvariables are specified as %{variable_name} and to output a normal% you just write them as %%. You can output a newline by using \n,a carriage return with \r and a tab space with \t.The output is by default written to standard output, but can bechanged with %{stderr} and %output{}.Output HTTP header values from the transfer's most recent serverresponse by using %header{name} where name is the case insensitivename of the header (without the trailing colon). The headercontents are exactly as delivered over the network but withleading and trailing whitespace and newlines stripped off (addedin 7.84.0).Select a specific target destination file to write the output to,by using %output{name} (added in curl 8.3.0) where name is thefull filename. The output following that instruction is thenwritten to that file. More than one %output{} instruction can bespecified in the same write-out argument. If the filename cannotbe created, curl leaves the output destination to the one usedprior to the %output{} instruction. Use %output{>>name} to appenddata to an existing file.This output is done independently of if the file transfer wassuccessful or not.If the specified action or output specified with this option failsin any way, it does not make curl return a (different) error.NOTE: On Windows, the %-symbol is a special symbol used to expandenvironment variables. In batch files, all occurrences of % mustbe doubled when using this option to properly escape. If thisoption is used at the command prompt then the % cannot be escapedand unintended expansion is possible.The variables available are:certsOutput the certificate chain with details. Supported only bythe OpenSSL, GnuTLS, Schannel, Rustls, and Secure Transportbackends. (Added in 7.88.0)conn_idThe connection identifier last used by the transfer. Theconnection id is unique number among all connections using thesame connection cache. (Added in 8.2.0)content_typeThe Content-Type of the requested document, if there was any.errormsgThe error message. (Added in 7.75.0)exitcodeThe numerical exit code of the transfer. (Added in 7.75.0)filename_effectiveThe ultimate filename that curl writes out to. This is onlymeaningful if curl is told to write to a file with the--remote-name or --output option. It is most useful incombination with the --remote-header-name option.ftp_entry_pathThe initial path curl ended up in when logging on to theremote FTP server.header{name}The value of header "name" from the transfer's most recentserver response. Unlike other variables, the variable name"header" is not in braces. For example "%header{date}". Referto --write-out remarks. (Added in 7.84.0)header_jsonA JSON object with all HTTP response headers from the recenttransfer. Values are provided as arrays, since in the case ofmultiple headers there can be multiple values. (Added in7.83.0)The header names provided in lowercase, listed in order ofappearance over the wire. Except for duplicated headers. Theyare grouped on the first occurrence of that header, each valueis presented in the JSON array.http_codeThe numerical response code that was found in the lastretrieved HTTP(S) or FTP(s) transfer.http_connectThe numerical code that was found in the last response (from aproxy) to a curl CONNECT request.http_versionThe http version that was effectively used.jsonA JSON object with all available keys. (Added in 7.70.0)local_ipThe IP address of the local end of the most recently doneconnection - can be either IPv4 or IPv6.local_portThe local port number of the most recently done connection.methodThe http method used in the most recent HTTP request. (Addedin 7.72.0)num_certsNumber of server certificates received in the TLS handshake.Supported only by the OpenSSL, GnuTLS, Schannel, Rustls andSecure Transport backends. (Added in 7.88.0)num_connectsNumber of new connects made in the recent transfer.num_headersThe number of response headers in the most recent request(restarted at each redirect). Note that the status line IS NOTa header. (Added in 7.73.0)num_redirectsNumber of redirects that were followed in the request.num_retriesNumber of retries actually performed when "--retry" has beenused. (Added in 8.9.0)onerrorThe rest of the output is only shown if the transfer returneda non-zero error. (Added in 7.75.0)output{filename}From this point on, the --write-out output is written to thefilename specified in braces. The filename can be prefixedwith ">>" to append to the file. Unlike other variables, thevariable name "output" is not in braces. For example"%output{>>stats.txt}". Refer to --write-out remarks. (Addedin 8.3.0)proxy_ssl_verify_resultThe result of the HTTPS proxy's SSL peer certificateverification that was requested. 0 means the verification wassuccessful.proxy_usedReturns 1 if the previous transfer used a proxy, otherwise 0.Useful to for example determine if a "NOPROXY" pattern matchedthe hostname or not. (Added in 8.7.0)redirect_urlWhen an HTTP request was made without --location to followredirects (or when --max-redirs is met), this variable showsthe actual URL a redirect would have gone to.refererThe Referer: header, if there was any. (Added in 7.76.0)remote_ipThe remote IP address of the most recently done connection -can be either IPv4 or IPv6.remote_portThe remote port number of the most recently done connection.response_codeThe numerical response code that was found in the lasttransfer (formerly known as "http_code").schemeThe URL scheme (sometimes called protocol) that waseffectively used.size_downloadThe total amount of bytes that were downloaded. This is thesize of the body/data that was transferred, excluding headers.size_headerThe total amount of bytes of the downloaded headers.size_requestThe total amount of bytes that were sent in the HTTP request.size_uploadThe total amount of bytes that were uploaded. This is the sizeof the body/data that was transferred, excluding headers.speed_downloadThe average download speed that curl measured for the completedownload. Bytes per second.speed_uploadThe average upload speed that curl measured for the completeupload. Bytes per second.ssl_verify_resultThe result of the SSL peer certificate verification that wasrequested. 0 means the verification was successful.stderrFrom this point on, the --write-out output is written tostandard error. (Added in 7.63.0)stdoutFrom this point on, the --write-out output is written tostandard output. This is the default, but can be used toswitch back after switching to stderr. (Added in 7.63.0)time_appconnectThe time, in seconds, it took from the start until theSSL/SSH/etc connect/handshake to the remote host wascompleted.time_connectThe time, in seconds, it took from the start until the TCPconnect to the remote host (or proxy) was completed.time_namelookupThe time, in seconds, it took from the start until the nameresolving was completed.time_posttransferThe time it took from the start until the last byte is sent bylibcurl. In microseconds. (Added in 8.10.0)time_pretransferThe time, in seconds, it took from the start until the filetransfer was just about to begin. This includes allpre-transfer commands and negotiations that are specific tothe particular protocol(s) involved.time_queueThe time, in seconds, the transfer was queued during its run.This adds the queue time for each redirect step that may havehappened. Transfers may be queued for significant amounts oftime when connection or parallel limits are in place. (Addedin 8.12.0)time_redirectThe time, in seconds, it took for all redirection stepsincluding name lookup, connect, pretransfer and transferbefore the final transaction was started. "time_redirect"shows the complete execution time for multiple redirections.time_starttransferThe time, in seconds, it took from the start until the firstbyte was received. This includes time_pretransfer and also thetime the server needed to calculate the result.time_totalThe total time, in seconds, that the full operation lasted.tls_earlydataThe amount of bytes that were sent as TLSv1.3 early data. Thisis 0 if this TLS feature was not used and negative if the datasent had been rejected by the server. The use of early data isenabled via the command line option "--tls-earlydata". (Addedin 8.12.0)urlThe URL that was fetched. (Added in 7.75.0)url.schemeThe scheme part of the URL that was fetched. (Added in 8.1.0)url.userThe user part of the URL that was fetched. (Added in 8.1.0)url.passwordThe password part of the URL that was fetched. (Added in8.1.0)url.optionsThe options part of the URL that was fetched. (Added in 8.1.0)url.hostThe host part of the URL that was fetched. (Added in 8.1.0)url.portThe port number of the URL that was fetched. If no port numberwas specified and the URL scheme is known, that scheme'sdefault port number is shown. (Added in 8.1.0)url.pathThe path part of the URL that was fetched. (Added in 8.1.0)url.queryThe query part of the URL that was fetched. (Added in 8.1.0)url.fragmentThe fragment part of the URL that was fetched. (Added in8.1.0)url.zoneidThe zone id part of the URL that was fetched. (Added in 8.1.0)urle.schemeThe scheme part of the effective (last) URL that was fetched.(Added in 8.1.0)urle.userThe user part of the effective (last) URL that was fetched.(Added in 8.1.0)urle.passwordThe password part of the effective (last) URL that wasfetched. (Added in 8.1.0)urle.optionsThe options part of the effective (last) URL that was fetched.(Added in 8.1.0)urle.hostThe host part of the effective (last) URL that was fetched.(Added in 8.1.0)urle.portThe port number of the effective (last) URL that was fetched.If no port number was specified, but the URL scheme is known,that scheme's default port number is shown. (Added in 8.1.0)urle.pathThe path part of the effective (last) URL that was fetched.(Added in 8.1.0)urle.queryThe query part of the effective (last) URL that was fetched.(Added in 8.1.0)urle.fragmentThe fragment part of the effective (last) URL that wasfetched. (Added in 8.1.0)urle.zoneidThe zone id part of the effective (last) URL that was fetched.(Added in 8.1.0)urlnumThe URL index number of this transfer, 0-indexed. UnglobbedURLs share the same index number as the origin globbed URL.(Added in 7.75.0)url_effectiveThe URL that was fetched last. This is most meaningful if youhave told curl to follow location: headers.xfer_idThe numerical identifier of the last transfer done. -1 if notransfer has been started yet for the handle. The transfer idis unique among all transfers performed using the sameconnection cache. (Added in 8.2.0)If --write-out is provided several times, the last set value isused.Example:curl -w '%{response_code}\n' https://example.comSee also --verbose and --head.
评论
发表评论