最近在API對接的時候遇到一些問題,記錄下。
API為HTTP GET/POST方式,但對方是使用C/C++自行封裝的HTTP協議。
1、nginx在HTTP1.1下預設開啟chunk模式,header中不包含Content-length,而在訊息體中會多出一些用於chunk解碼的字元。
簡易擷取資料的話,還是用Content-length比較方便,這就需要處理兩個地方:
- 程式返回前手動加上Content-length的header
- Nginx配置中在需要的location中關閉chunk模式
location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:10080; fastcgi_index index.php; include fastcgi.conf; chunked_transfer_encoding off; //這一句 }
2、向Apache或Nginx發送POST請求時,需要在header中加上 Content-Type: application/x-www-form-urlencoded
Apache和Nginx是通過它擷取POST內容的,如果header中該參數不正確,則無法取到POST資料。
----------------------------------------------我是分割線-----------------------------------------------
參考資料:
http://www.cnblogs.com/pingf/archive/2009/06/26/1511807.html
http://blog.csdn.net/minibar/article/details/1358442
http://www.bixuda.com/2010/08/30/nginxphp%E4%B8%8B%E7%94%A8content-length%E6%9B%BF%E6%8D%A2chunk%E7%BC%96%E7%A0%81%E6%A8%A1%E5%BC%8F%E6%96%B9%E6%B3%95/