我正在尝试做 WEB 简单的 restful 服务。我的代码基于这篇文章: 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}";
}
它适用于英文符号,但我需要使用西里尔符号。 如果我在 url 中使用西里尔符号,它会返回 404 错误。 我尝试使用百分比编码但结果是一样的 这是我的客户端代码:
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();
}
有什么办法解决这个问题吗?
更新:我找到了一个解决方案——只是用数字代码替换了非英语符号,但我想找到更优雅的解决方案
请您参考如下方法:
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
responseHeaderEncoding="utf-8"
fileEncoding="utf-8"
resourceProviderFactoryType=""
enableBestFitResponseEncoding="true"/>
在 web.config 全局化标签中添加这些参数
然后使用类似这样的东西
//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)
希望对你有帮助
