2012-12-27 45 views
1

我有我的REST Web服務,它應接收帶有西裏爾字母的GET編碼請求。REST Web服務中的西裏爾字母GET請求

例如:?www.service/SRV參數1 =%D1%E0%ED%EA%F2

我知道,這是Windows的1251 ISO-8859-1,但作爲輸入參數的值在我的網絡服務功能中,總是有類似問號的東西。我覺得該服務將字符串轉換爲UTF-8。

是否有可能在Windows-1251代碼頁中recive GET請求?

還有一個類似的線程:Cyrillic letters are incorrectly encoded in the C# Web Service 答案是使用utf-8編碼。但即時通訊我的情況下,我不能改變請求到Web服務。

Web服務描述:

[OperationContract] 
    [WebInvoke(Method="GET", ResponseFormat=WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Bare, 
      UriTemplate = @"param?p1={p1}&p2={p2}&p3={p3}…")] 

    string MyFunction(string p1, string p2, string p3, …); 
+0

我剛纔想到的解決辦法是防止自動解碼GET參數。如果只有函數可以得到原始的編碼參數,解碼它沒什麼大不了的。但我不知道這是否可能。 –

+0

如何從URI的查詢字符串中讀取參數? –

+0

@SimonMourier使用WebInvoke –

回答

1

只有我可以拿出的解決方案是:

PropertyInfo[] inf = WebOperationContext.Current.IncomingRequest.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance); 
       HttpRequestMessageProperty val = (HttpRequestMessageProperty)inf[0].GetValue(WebOperationContext.Current.IncomingRequest, null); 
       string paramString = HttpUtility.UrlDecode(val.QueryString, Encoding.GetEncoding(1251)); 
       Uri address = new Uri("http://server.ru/services/service.svc/reg?" + paramString); 

       p1 = HttpUtility.ParseQueryString(address.Query).Get("p1"); 
       p2 = HttpUtility.ParseQueryString(address.Query).Get("p2"); 
       p3 = HttpUtility.ParseQueryString(address.Query).Get("p3"); 
       ... 

我不知道爲什麼全球化標籤沒有在這種情況下工作。 儘管此代碼有效,但我非常感謝有關此問題的任何進一步建議。

0

你可以試着改變你的web.config這樣的:

<system.web> 
    <globalization requestEncoding="iso-8859-1" ... other stuff... /> 
</system.web> 

注意它可能對您的應用程序的一些其他副作用。

+0

我嘗試過使用Windows-1251,現在試着按照你的建議使用iso-8859-1。但仍然沒有效果。 –