<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]curl命令获取HTTP头文件，Curl下面用Post加-d访问后无法单独获取PUT方式的HTTP头，得用curl -s -dtest=test -D- -o/tmp/null.txt http://xxxxxxxx.com/post.php 。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Mon, 08 Apr 2013 02:33:38 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	经实践和抓包测试发现，不能将-d和-I一起用的原因是-d是POST方式，而 -I 则是PUT方式，且-I并没有内容返回，只是返回服务器的Header头部信息，两者混用就会出现矛盾，所以提示：Warning: You can only select one HTTP request method! You asked for both POST<br/>Warning: (-d, --data) and HEAD (-I, --head).<br/><br/>一、下面这中方法，CURL的方式进行Post数据同时，并显示出头部Header信息：<br/>curl -s -d&#039;test=test&#039; -D- -o/tmp/null.txt http://coding.jackxiang.com/src-ui/postcaptcha.php&nbsp;&nbsp; #注意-D 后面的-，表示输出“文件”是标准输出。<br/>上面命令行细节如下：-D- ：D后面有一个中间杠：<br/>-s 不输出：Total&nbsp;&nbsp;&nbsp;&nbsp;% Received % Xferd&nbsp;&nbsp;Average Speed&nbsp;&nbsp; Time&nbsp;&nbsp;&nbsp;&nbsp;Time&nbsp;&nbsp;&nbsp;&nbsp; Time&nbsp;&nbsp;Current信息<br/><br/>HTTP/1.1 200 OK<br/>Server: openresty<br/>Date: Tue, 30 May 2023 08:19:58 GMT<br/>Content-Type: text/html; charset=UTF-8<br/>Transfer-Encoding: chunked<br/>Connection: keep-alive<br/>Vary: Accept-Encoding<br/>Vary: Accept-Encoding<br/><br/>cat /tmp/null.txt<br/>Array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;[test] =&gt; test<br/>)<br/>======Wireshark抓包如下(有数据从服务器上返回）======<br/>经实测发现并不是真正的Head方法：<br/><textarea name="code" class="php" rows="15" cols="100">
POST /src-ui/postcaptcha.php HTTP/1.1
Host: coding.jackxiang.com
User-Agent: curl/7.87.0
Accept: */*
Content-Length: 9
Content-Type: application/x-www-form-urlencoded
test=testHTTP/1.1 200 OK
Server: openresty
Date: Tue, 30 May 2023 08:10:56 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding

Array
(
&nbsp;&nbsp;&nbsp;&nbsp;[test] =&gt; test
)
</textarea><br/><br/>二、而如果真用Head方法，则是这样的：<br/>curl -I http://coding.jackxiang.com/src-ui/postcaptcha.php<br/>HTTP/1.1 200 OK<br/>Server: openresty<br/>Date: Tue, 30 May 2023 08:21:39 GMT<br/>Content-Type: text/html; charset=UTF-8<br/>Connection: keep-alive<br/>Vary: Accept-Encoding<br/>Vary: Accept-Encoding<br/><br/>======Wireshark抓包如下(只返回Header头，没有其它数据从服务器上返回）======<br/>HEAD /src-ui/postcaptcha.php HTTP/1.1<br/>Host: coding.jackxiang.com<br/>User-Agent: curl/7.87.0<br/>Accept: */*<br/><br/>HTTP/1.1 200 OK<br/>Server: openresty<br/>Date: Tue, 30 May 2023 08:16:52 GMT<br/>Content-Type: text/html; charset=UTF-8<br/>Connection: keep-alive<br/>Vary: Accept-Encoding<br/>Vary: Accept-Encoding<br/><br/><br/>POST的demo代码基础测试：<br/>curl -d&quot;test=test&quot; http://coding.jackxiang.com/src-ui/postcaptcha.php<br/>Array<br/>(<br/>&nbsp;&nbsp;&nbsp;&nbsp;[test] =&gt; test<br/>)<br/>postcaptcha.php<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$postArr = initPostData();
print_r($postArr);
//var_dump($postArr);

/**
&nbsp;&nbsp;&nbsp;&nbsp; * 获取 post 参数; 在 content_type 为 application/json 时，自动解析 json
&nbsp;&nbsp;&nbsp;&nbsp; * @return array
&nbsp;&nbsp;&nbsp;&nbsp; */
function initPostData()
&#123;
&nbsp;&nbsp; $headers = getallheaders();
&nbsp;&nbsp; if(false !== strpos($headers[&#039;Content-Type&#039;], &#039;application/json&#039;))&#123;&nbsp;&nbsp;//Json的POST表单提交
&nbsp;&nbsp;&nbsp;&nbsp; $content = file_get_contents(&#039;php://input&#039;);
&nbsp;&nbsp;&nbsp;&nbsp; $post&nbsp;&nbsp;&nbsp;&nbsp;= (array)json_decode($content, true);&nbsp;&nbsp; //返回json转成PHP的array
&nbsp;&nbsp; &#125;else&#123;&nbsp;&nbsp;//常规表单提交application/x-www-form-urlencoded
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$post = $_POST;
&nbsp;&nbsp; &#125;
&nbsp;&nbsp; return $post;
&#125;
</textarea><br/><br/>curl中的-I选项使用HEAD方法自动发出http请求，是指哪些请求呢？<br/>在cURL中，使用-I选项会发出一个HEAD方法的HTTP请求。HEAD方法是HTTP协议中的一种请求方法，它类似于GET方法，但服务器只返回响应头部信息而不返回实际的响应内容主体。<br/>当你使用curl -I &lt;URL&gt;命令时，-I选项会告诉cURL发送一个HEAD请求，并返回服务器对该请求的响应头部信息。这对于获取有关资源的元数据、检查资源的状态或验证资源是否存在等场景非常有用，而无需获取整个响应内容。<br/>请注意，由于HEAD方法只返回响应头部信息，因此响应的主体部分为空。这使得HEAD请求比GET请求更轻量级，因为它不需要传输和处理响应的实际内容。<br/>总结起来，curl -I &lt;URL&gt;会发送一个HEAD请求，并返回服务器对该请求的响应头部信息，而不返回响应的主体内容。<br/>[/codes]<br/><br/><br/><br/><br/>附：<br/>The -I option tells curl to do a HEAD request while the -d&#039;test=test&#039; option tells curl to do a POST, so you&#039;re telling curl to do two different request types.<br/>curl -s -d&#039;test=test&#039; -D- -o/dev/null http://coding.jackxiang.com/src-ui/postcaptcha.php<br/>or, on Windows:<br/>curl -s -d&#039;test=test&#039; -D- -onul: http://coding.jackxiang.com/src-ui/postcaptcha.php<br/><br/>背景：有时一个页面莫名奇妙变为:您请求的页面发生错误！ 这显然是经过http做的http头的redirect，于是否怎么知道呢？用curl<br/>curl命令获取HTTP头文件：<br/><textarea name="code" class="C" rows="15" cols="100">
C:&#92;Users&#92;admin&gt;curl --head &quot;http://xiyou.cntv.cn/v-004c2e80-39fb-11e2-b474-a4badb4689bc.html&quot;
HTTP/1.1 302 Found
Date: Mon, 08 Apr 2013 02:31:45 GMT
Server: Apache/2.2.22 (Win32) PHP/5.3.13
X-Powered-By: PHP/5.3.13
Set-Cookie: KOSSID=21f2tgn7sumfd0nqllsgckfj10; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: http://xiyou.cntv.cn/error.html
Content-Type: text/html; charset=utf-8

</textarea><br/><br/>看下Cookie吧：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$name = &quot;language1&quot;;
$value = &quot;ZH&quot;;
$expiration = time()-3600;
echo setcookie($name, $value, $expiration);

$value = &quot;EN&quot;;
$expiration = time()+3600;
echo setcookie($name, $value, $expiration);
echo setcookie($name, $value, $expiration);

</textarea><br/>用Curl试试看，主要是看Cookie的过期时间，是怎么发送的：<br/><textarea name="code" class="php" rows="15" cols="100">
C:&#92;Users&#92;admin&gt;curl --head &quot;http://w.xiyou.cntv.cn/&quot;
HTTP/1.1 200 OK
Date: Thu, 15 May 2014 09:19:01 GMT
Server: Apache/2.2.22 (Win32) PHP/5.3.13
X-Powered-By: PHP/5.3.13
Set-Cookie: language1=ZH; expires=Thu, 15-May-2014 08:19:01 GMT
Set-Cookie: language1=EN; expires=Thu, 15-May-2014 10:19:01 GMT
Set-Cookie: language1=EN; expires=Thu, 15-May-2014 10:19:01 GMT
Content-Type: text/html
</textarea><br/><br/>一）其实用HttpWatcher也能看到Http给浏览器发送的头的Cookie时间，如下：<br/><textarea name="code" class="php" rows="15" cols="100">
(Status-Line)&nbsp;&nbsp;HTTP/1.1 200 OK
Connection&nbsp;&nbsp;Keep-Alive
Content-Length&nbsp;&nbsp;527
Content-Type&nbsp;&nbsp;text/html
Date&nbsp;&nbsp;Thu, 15 May 2014 09:30:23 GMT
Keep-Alive&nbsp;&nbsp;timeout=5, max=100
Server&nbsp;&nbsp;Apache/2.2.22 (Win32) PHP/5.3.13
Set-Cookie&nbsp;&nbsp;language1=ZH; expires=Thu, 15-May-2014 08:30:23 GMT
Set-Cookie&nbsp;&nbsp;language1=EN; expires=Thu, 15-May-2014 10:30:23 GMT
Set-Cookie&nbsp;&nbsp;language1=EN; expires=Thu, 15-May-2014 10:30:23 GMT
X-Powered-By&nbsp;&nbsp;PHP/5.3.13
</textarea><br/>二）用Fiddler2也能看，在返回（下面），选 header也能看到Cookie的过期时间。（想了解整个可以在Url上右键，后选：Copy --&gt; Header Only。）<br/>三）用Firefox下的firebug也能看到时间。<br/>四）Chrome也能看，F12调出开发者工具后，Resources Cookies 域名里就有它。（它对Cookie的时间作了列分割，前面都是大大的显示出来。）<br/><br/>看到了吧：Location: http://w.xiyou.cntv.cn/error.html&nbsp;&nbsp;，它就是转向的根源，它向浏览器发送了这样一个http的头，于是你就看到了这个转向了的页面。<br/><br/><br/><br/>以下来自：<br/>http://blog.csdn.net/anljf/article/details/6858599<br/><br/># curl -I URL<br/>该选项功能：<br/>&nbsp;&nbsp;-I/--head<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(HTTP/FTP/FILE)&nbsp;&nbsp;Fetch&nbsp;&nbsp;the&nbsp;&nbsp;HTTP-header only! HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document. When used on a FTP or<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FILE file, curl displays the file size and last modification time only.<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If this option is used twice, the second will again disable header only.<br/><br/>响应消息<br/>响应消息的第一行为下面的格式：<br/>HTTP-VersionSPStatus-CodeSPReason-PhraseCRLF<br/>HTTP -Version表示支持的HTTP版本，例如为HTTP/1.1。<br/>Status-Code是一个三个数字的结果代码。<br/>Reason-Phrase给 Status-Code提供一个简单的文本描述。<br/>Status-Code主要用于机器自动识别，Reason-Phrase主要用于帮助用户理解。 <br/>Status-Code的第一个数字定义响应的类别，后两个数字没有分类的作用。第一个数字可能取5个不同的值：<br/>1xx:信息响应类，表示接收到请求并且继续处理<br/>2xx:处理成功响应类，表示动作被成功接收、理解和接受<br/>3xx:重定向响应类，为了完成指定的动作，必须接受进一步处理<br/>4xx:客户端错误，客户请求包含语法错误或者是不能正确执行<br/>5xx:服务端错误，服务器不能正确执行一个正确的请求<br/><br/>典型的响应消息：<br/>HTTP/1.0200OK<br/>Date:Mon,31Dec200104:25:57GMT<br/>Server:Apache/1.3.14(Unix)<br/>Content-type:text/html<br/>Last-modified:Tue,17Apr200106:46:28GMT<br/>Etag:&quot;a030f020ac7c01:1e9f&quot;<br/>Content-length:39725426<br/>Content-range:bytes554554-40279979/40279980<br/>或<br/>HTTP/1.1 200 OK<br/>Server: nginx/1.0.4<br/>Date: Mon, 10 Oct 2011 01:36:14 GMT<br/>Content-Type: text/html; charset=utf-8<br/>Connection: keep-alive<br/>Vary: Accept-Encoding<br/>X-Powered-By: PHP/5.3.6<br/>Set-Cookie: PHPSESSID=6a2vrl3c7399av3nge4ti66432; path=/<br/>Expires: Thu, 19 Nov 1981 08:52:00 GMT<br/>Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0<br/>Pragma: no-cache<br/>上例第一行表示HTTP服务端响应一个GET方法<br/>Date头域<br/>Date头域表示消息发送的时间，时间的描述格式由rfc822定义。例如，Date:Mon,31Dec200104:25:57GMT。Date描述的时间表示世界标准时，换算成本地时间，需要知道用户所在的时区。<br/>Vary: Accept-Encoding：浏览器能够进行解码的数据编码方式<br/>Connection： 表示是否需要持久连接。如果Servlet看到这里的值为“Keep-Alive”，或者看到请求使用的是HTTP 1.1（HTTP 1.1默认进行持久连接）<br/>Cookie：这是最重要的响应头信息之一<br/>Pragma头域<br/>Pragma：指定“no-cache”值表示服务器必须返回一个刷新后的文档，即使它是代理服务器而且已经有了页面的本地拷贝，最常用的是Pragma:no-cache。在HTTP/1.1协议中，它的含义和Cache-Control:no-cache相同。<br/>Cache-Control头域<br/>Cache-Control指定请求和响应遵循的缓存机制。<br/>请求时的缓存指令包括no-cache、no -store、max-age、max-stale、min-fresh、only-if-cached，<br/>响应消息中的指令包括public、 private、no-cache、no-store、no-transform、must-revalidate、proxy-revalidate、 max-age。<br/>各个消息中的指令含义如下：<br/>Public指示响应可被任何缓存区缓存。<br/>Private指示对于单个用户的整个或部分响应消息，不能被共享缓存处理。这允许服务器仅仅描述当用户的部分响应消息，此响应消息对于其他用户的请求无效。<br/>no-cache指示请求或响应消息不能缓存<br/>no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。<br/>max-age指示客户机可以接收生存期不大于指定时间（以秒为单位）的响应。<br/>min-fresh指示客户机可以接收响应时间小于当前时间加上指定时间的响应。<br/>max-stale指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值，那么客户机可以接收超出超时期指定值之内的响应消息。<br/>Location响应头<br/>Location响应头用于重定向接收者到一个新URI地址。<br/>Server响应头<br/>Server响应头包含处理请求的原始服务器的软件信息。此域能包含多个产品标识和注释，产品标识一般按照重要性排序。<br/>实体<br/>请求消息和响应消息都可以包含实体信息，实体信息一般由实体头域和实体组成。实体头域包含关于实体的原信息，<br/>实体头包括Allow、Content- Base、Content-Encoding、Content-Language、Content-Length、Content-Location、 Content-MD5、Content-Range、Content-Type、Etag、Expires、Last-Modified、 extension-header。<br/>extension-header允许客户端定义新的实体头，但是这些域可能无法未接受方识别。<br/>实体可以是一个经过编码的字节流，它的编码方式由Content-Encoding或Content-Type定义，它的长度由Content-Length或Content -Range定义。<br/><br/>Content-Type实体头用于向接收方指示实体的介质类型，指定HEAD方法送到接收方的实体介质类型，或GET方法发送的请求介质类型Content-Range实体头<br/>Content-Range实体头用于指定整个实体中的一部分的插入位置，他也指示了整个实体的长度。在服务器向客户返回一个部分响应，它必须描述响应覆盖的范围和整个实体长度。一般格式：<br/>Content-Range:bytes-unitSPfirst-byte-pos-last-byte-pos/entity-legth<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 例如，传送头500个字节次字段的形式：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Content-Range:bytes0-499/1234如果一个http消息包含此节（例如，对范围请求的响应或对一系列范围的重叠请求），Content-Range表示传送的范围，Content-Length表示实际传送的字节数。<br/>Last-modified实体头<br/>Last-modified实体头指定服务器上保存内容的最后修订时间。
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]curl命令获取HTTP头文件，Curl下面用Post加-d访问后无法单独获取PUT方式的HTTP头，得用curl -s -dtest=test -D- -o/tmp/null.txt http://xxxxxxxx.com/post.php 。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>