url中的百分比编码WebRequest 404错误RESTful服务

时间:2012-11-09 06:17:36

标签: c# .net wcf rest

我正在尝试做简单的WEB简单服务。我的代码基于这篇文章: http://www.progware.org/Blog/post/A-simple-REST-service-in-C.aspx 我稍微改了一下,所以我的服务可以获得几个参数。 以下是我的更改:

 public string GetClientNameById(string LS, string Owner, string info)
{
 .....
 return System.IO.File.ReadAllText(XMLFileName, Encoding.GetEncoding(1251));
}
[ServiceContract(Name = "RESTServices")]
    public interface IRESTServices
    {
        [OperationContract]
        [WebGet(UriTemplate = Routing.GetEPDRoute, BodyStyle = WebMessageBodyStyle.Bare)]
        string GetClientNameById(string Id,string LS,string tmp);
    }
public static class Routing
    {
        public const string GetEPDRoute = "/EPD/{id}+{LS}+{tmp}";
    }

它适用于英语simbols,但我需要使用西里尔语符号。 如果我在url中使用西里尔字符串,则会返回404错误。 我尝试使用Percent-encoding但结果是一样的 这是我的客户代码:

string url = "http://localhost:4385/Service.svc/EPD/995118466+" + System.Uri.EscapeDataString("Аксенова") + "+434";
            WebRequest req = WebRequest.Create(url);
            using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(resp.GetResponseStream());
                label1.Text = reader.ReadToEnd();
            } 

任何想法如何解决这个问题?

更新:我找到了一个解决方案 - 用数字代码替换非英语simbols,但我想找到更优雅的解决方案

1 个答案:

答案 0 :(得分:0)

<globalization
      requestEncoding="utf-8"
      responseEncoding="utf-8"
      responseHeaderEncoding="utf-8"
      fileEncoding="utf-8"
      resourceProviderFactoryType=""
      enableBestFitResponseEncoding="true"/>

在web.config全球标记

中添加thease参数

然后使用类似的东西

//UTF-8
HttpUtility.UrlDecode(parameter, System.Text.Encoding.UTF8)
//UTF-16
HttpUtility.UrlDecode(parameter, System.Text.Encoding.Unicode)
//UTF-32
HttpUtility.UrlDecode(parameter, System.Text.Encoding.UTF32)

希望有所帮助

相关问题