当我在查询字符串中输入一些 Unicode (UTF-8) 字符时,它会在我的程序中发生变化。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestServices : IRESTServices
{
public Stream GetClientNameById(string id, string message)
{
string ReturnString = string.Format("id={0} and message ={1}", id, message);
return GenerateStreamFromString(ReturnString);
}
private Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream, Encoding.GetEncoding("utf-8"));
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
}
如果我输入以下地址,消息将更改:
http://localhost:8000/DEMOService/subscribe?userID=14953828&message=مرحبا
它将更改为 ÓáÇã
如何使用 utf-8?
这是界面:
[ServiceContract(Name = "RESTDemoServices")]
[XmlSerializerFormat]
public interface IRESTServices
{
[OperationContract]
[WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
Stream GetClientNameById(string id, string message);
}
和:
public static class Routing
{
public const string GetClientRoute = "/subscribe?userID={id}&message={message}";
}