Nginx使用反向代理時Hessian411錯誤解決方案(轉)


問題描述:

用 Hessian 實現 web service 過程中,需要創建對象時,是使用 HTTP POST 方法來傳遞數據的。但是在有反向代理 (nginx) 的情況下,會拋出異常 (com.caucho.hessian.client.HessianConnectionException: 411:java.io.IOException: Server returned HTTP response code: 411 for URL:http://xxxx/xxx/xxxService) 。

首先來看下 HTTP 411 錯誤的解釋: Length Required 服務器不能處理請求,除非客戶發送一個 Content-Length 頭。( HTTP 1.1 新)這是因爲 Hessian 與服務端通信默認是採取分塊的方式 (chunked encoding) 發送數據,而反向代理要獲得 Content-Length 這個頭,才能處理請求,但是 Hessian 的請求中並沒有加入這個參數。

解決方法:

com.caucho.hessian.client.HessianProxyFactory 類中,有一個 boolean _chunckedPost 的域成員,其默認值爲 true 。這個值就是規定 Hessian 是否以分塊發送的方式與服務端交換數據的參數,因此在創建com.caucho.hessian.client.HessianProxyFactory 的對象後(假設爲 factory ),只要調用其上的setChunckedPost() 方法,把這個屬性設置爲 false 即可。即 factory.setChunkedPost(false);

分塊編碼傳輸:

分塊編碼 (chunked encoding) 傳輸方式是 HTTP 1.1 協議中定義的 Web 用戶向服務器提交數據的一種方法,當服務器收到 chunked 編碼方式的數據時會分配一個緩衝區存放之,如果提交的數據大小未知,客戶端會以一個協商好的分塊大小向服務器提交數據。

The content can be broken up into a number of chunks; each of which is prefixed by its size in bytes. A zero size chunk indicates the end of the response message. If a server is using chunked encoding it must set the Transfer-Encoding header to "chunked".

Chunked encoding is useful when a large amount of data is being returned to the client and the total size of the response may not be known until the request has been fully processed. An example of this is generating an HTML table of results from a database query. If you wanted to use the Content-Length header you would have to buffer the whole result set before calculating the total content size. However, with chunked encoding you could just write the data one row at a time and write a zero sized chunk when the end of the query was reached.

如果不使用 Chunked encoding 傳輸方式,需要將要發送的數據緩存下來,計算出 content-length ,從而滿足反向代理( Nginx )需要 content-length 的要求。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章