今天遇到幾個PUT上傳的點,但是都沒利用起來。一怒之下,在自己本地試了一下。步驟如下:
一、環境:
首先,根據 配置Apache服務器支持向目錄PUT文件 更新一下httpd.conf文件,重啟所有服務。
二、HTTP - PUT
PUT的前提,是了解HTTP協議。下面給出HTTP - PUT的一個模板:
PUT /test.txt HTTP/1.1
Accept: */*
Accept-Language: en-US
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32)
Host: test.com:8080
hello world
要注意如下幾個點:
① PUT方法是HTTP 1.1協議中才出現的。
② HTTP協議對空格敏感,每行數據的結尾不能出現空格
③ HTTP頭部和數據中間要空一行,即HTTP頭部是以\r\n\r\n結尾的。
④ 端口號直接跟在HOST后面
三、第一次PUT
好了,現在可以PUT了。用Linux下的nc命令來進行連接。
nc -v www.baidu.com 80
root@bb:/etc# nc -v 192.168.163.1 8080 nc: 192.168.163.1 (192.168.163.1) 8080 [http-alt] open
/*在得到如上輸出之后,再將PUT包的內容貼到命令行中,回車*/ PUT /test.txt HTTP/1.1 Content-Length: 11 Accept-Language: en-US User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32) Host: 192.168.163.1:8080 hello world
/*發送的PUT包到此為止,以下為收到的數據包*/ HTTP/1.1 500 Internal Server Error Date: Wed, 29 Apr 2015 09:15:18 GMT Server: Apache/2.2.21 (Win64) PHP/5.3.10 DAV/2 Content-Length: 535 Connection: close Content-Type: text/html; charset=iso-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>500 Internal Server Error</title> </head><body> <h1>Internal Server Error</h1> <p>The server encountered an internal error or misconfiguration and was unable to complete your request.</p> <p>Please contact the server administrator, admin@localhost and inform them of the time the error occurred,and anything you might have done that may have caused the error.</p> <p>More information about this error may be available in the server error log.</p> </body></html>
好像不對啊,500是服務器內部錯誤啊。好吧好吧,我們去看看主機的Apache日志。
四、再次配環境
主機用的是WAMP,日志在/wamp/logs/apache_error.log。找到對應的那一條:
通過瀏覽器搜索,又找到一篇日志。Linux(CentOS) 服務端搭建與配置,原來用HTTP - PUT還要添加一個什么鎖。
所以解決方法是:在Apache配置文件中,也就是httpd.conf,增加一行,設定webDAV鎖位置。
再次重啟所有服務。
DavLockDB DavLock #目錄沒有就創建(DavLock 為文件名,應該有的目錄結構是\wamp\bin\apache\Apache2.2.21\DavLock)
五、終於成功PUT
root@bb:/etc# nc -v 192.168.163.1 8080 nc: 192.168.163.1 (192.168.163.1) 8080 [http-alt] open PUT /test.txt HTTP/1.1 Content-Length: 11 Accept-Language: en-US User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32) Host: 192.168.163.1:8080 hello world HTTP/1.1 201 Created Date: Wed, 29 Apr 2015 09:25:24 GMT Server: Apache/2.2.21 (Win64) PHP/5.3.10 DAV/2 Location: http://192.168.163.1:8080/test.txt Content-Length: 181 Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>201 Created</title> </head><body> <h1>Created</h1> <p>Resource /test.txt has been created.</p> </body></html>
0。0 差不多這樣,之后有更多關於PUT的經驗,再分享