2010-04-06 27 views
0

我有一個正在調用http restlet服務(put方法)的Perl客戶端。此調用中的某些參數包含日文文本。當我在restlet服務中打印這些請求參數的內容時,我發現這些字符亂碼!傳遞給http restlet服務時日文文本亂碼

這是我的PERL客戶端代碼:

my %request_headers = (
     'DocumentName' => $document_name, --> This name is a JAPANESE String 
     'DocumentDescription' => 'Test Japanese Chars', 
     'content-length' => 200, 
     'Content-Type' => 'application/octet-stream; charset=utf-8', 
     'User-Agent' => "JPCharTester", 
     ); 

     $s->write_request('PUT', '/test-document/TEST/TEST_DOCUMENT' , %request_headers, $content); 
在此調用$背景下雙方的價值觀和$ DOCUMENT_NAME是日本人字符串

。但是隻有document_name在我的後端服務中被接收爲亂碼。

這裏不用的服務代碼:

String URL_ENCODING = "UTF-8"; 
String documentName = requestHeaders.getFirstValue("DocumentName"); 
System.out.println("Encoded Document Name : "+documentName+" <<<"); --> documentName is garbled here 

try { 
    documentName = URLDecoder.decode(documentName, URL_ENCODING); 
    System.out.println(>>> Decoded Document Name : "+documentName+" <<<"); --> documentName is garbled here 
} catch (java.io.UnsupportedEncodingException ex) { 
    throwException(ex.getMessage(), Status.SERVER_ERROR_INTERNAL, ex); 
} 

上述兩個日誌語句印刷亂碼 !!

有人能告訴我我在做什麼錯誤以及如何解決這個問題?

在此先感謝您的幫助。

Regards, Satish。

+0

的可能的複製[什麼是字符編碼,爲什麼我要這麼做吧](https://stackoverflow.com/questions/10611455/what-is-character-encoding-and-why-應該-I-懶得與 - 它) – Raedwald 2017-10-26 22:50:57

回答

0

別忘了編碼客戶端的數據爲UTF-8。如果你說

'Content-Type' => 'application/octet-stream; charset=utf-8' 

比,不會神奇地解決問題。這只是接收器的一個提示,它將以哪種形式獲取數據。您還必須發送它與正確的格式。在Perl:

use utf8; 
... 
    'DocumentName' => utf8::encode($document_name), 
... 

... , utf8::encode($content) ...