Denis Kucherov
Denis Kucherov

Reputation: 379

Cyrillic letters GET request in REST web service

I have my REST web service that should receive GET encoded requests with cyrillic letters.

For example: www.service/srv?param1=%D1%E0%ED%EA%F2

I know that this is Windows-1251 ISO-8859-1, but as a value of input parameter in my web service function allways have something like question marks . I gues that service convert string to UTF-8.

Is it possible to recive GET request in Windows-1251 codepage?

There was a similar thread: Cyrillic letters are incorrectly encoded in the C# Web Service The answer was to use utf-8 encoding. But im my case I cant change request to web service.

Web service description:

[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, …);

Upvotes: 1

Views: 2455

Answers (2)

Denis Kucherov
Denis Kucherov

Reputation: 379

Only solution that I can come with is:

 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");
                ...

I'm wondered why globalization tag is not working in this case. Although this code works, I'm really appreciate any further suggestions on this matter.

Upvotes: 1

Simon Mourier
Simon Mourier

Reputation: 139065

You could try to change your web.config like this:

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

Note it could have some other side effects on your app.

Upvotes: 0

Related Questions