【问题标题】:Cyrillic letters GET request in REST web serviceREST Web 服务中的西里尔字母 GET 请求
【发布时间】:2012-12-27 10:13:49
【问题描述】:

我的 REST Web 服务应该接收带有西里尔字母的 GET 编码请求。

例如:www.service/srv?param1=%D1%E0%ED%EA%F2

我知道这是 Windows-1251 ISO-8859-1,但作为我的 Web 服务函数中的输入参数值,总是有问号之类的东西。我猜该服务将字符串转换为 UTF-8。

是否可以在 Windows-1251 代码页中接收 GET 请求?

有一个类似的帖子:Cyrillic letters are incorrectly encoded in the C# Web Service 答案是使用 utf-8 编码。但在我的情况下,我无法更改对 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, …);

【问题讨论】:

  • 我只是想可能的解决方案是防止自动解码 GET 参数。如果只有函数可以得到原始编码参数,解码它没有什么大不了的。但我不知道这是否可能。
  • 如何从 URI 的查询字符串中读取参数?
  • @SimonMourier 使用 WebInvoke

标签: c# asp.net web-services encoding character-encoding


【解决方案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");
                ...

我想知道为什么全球化标签在这种情况下不起作用。 尽管此代码有效,但我非常感谢有关此问题的任何进一步建议。

【讨论】:

    【解决方案2】:

    您可以尝试像这样更改您的 web.config:

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

    请注意,它可能会对您的应用产生一些其他副作用。

    【讨论】:

    • 我在 Windows-1251 上进行了尝试,现在按照您的建议尝试了 iso-8859-1。但仍然没有效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多