我在 WP7 中遇到编码问题。我向 API 发出 json 请求,响应包括德语变音符号,但由于某种原因,它显示不正确。这是发出请求的代码:
public static void makeRequest(string requestString,List<String> parameters,Action<string> handleRequestResult)
{
//generate a random nonce and a timestamp
Random rand = new Random();
Random rand2 = new Random();
string nonce1 = rand.Next().ToString();
string nonce2 = rand2.Next().ToString();
string nonce = nonce1 + nonce2;
string timestamp = GetTimestamp();
//create the parameter string in alphabetical order
//string parameters = "oauth_callback=" + UrlHelper.Encode("http://www.example.com");
parameters.Add("oauth_consumer_key=" + ConsumerKey);
parameters.Add("oauth_nonce=" + nonce);
parameters.Add("oauth_signature_method=HMAC-SHA1");
parameters.Add("oauth_timestamp=" + timestamp);
parameters.Add("oauth_version=1.0");
//sorting the list of parameters (adding '&' between them)
string sortedParameters = sortParams(parameters);
//generate a signature base on the current requeststring and parameters and adding it to request string
string signature = generateSignature("GET", requestString, sortedParameters);
string url = requestString + "?" + sortedParameters + "&oauth_signature=" + signature;
//test the request
WebClient web2 = new WebClient();
web2.Encoding = UTF8Encoding.UTF8;
//string result = web.DownloadString(url);
web2.DownloadStringCompleted += (sender, e) =>
{
string result = (string)e.Result;
handleRequestResult(result);
//App.ViewModel.LoadData(result);
};
web2.DownloadStringAsync(new Uri(url));
}
这是加载视图响应的示例代码:
public void LoadBlogPosts(string content)
{
if (!AddToExistingBlogs)
{
this.BlogPosts.Clear();
}
XDocument xml = XDocument.Parse(content);
foreach (XElement element in xml.Descendants("item"))
{
BlogViewModel newBlog = new BlogViewModel();
newBlog.Title = element.Element("title").Value;
newBlog.Description = element.Element("description").Value;
newBlog.Link = element.Element("link").Value;
newBlog.Category = element.Element("category").Value;
//newBlog.Comments = element.Element("comments").Value;
newBlog.PubDate = element.Element("pubDate").Value;
XNamespace dc = "http://purl.org/dc/elements/1.1/";
XNamespace wfw = "http://wellformedweb.org/CommentAPI/";
XNamespace atom = "http://www.w3.org/2005/Atom";
XNamespace sy = "http://purl.org/rss/1.0/modules/syndication/";
XNamespace slash = "http://purl.org/rss/1.0/modules/slash/";
XNamespace contentn = "http://purl.org/rss/1.0/modules/content/";
newBlog.Dccreator = element.Element(dc + "creator").Value;
newBlog.Guid = element.Element("guid").Value;
newBlog.WfwCommentRss = element.Element(wfw + "commentRss").Value;
newBlog.SlashComments = element.Element(slash + "comments").Value;
newBlog.Content = "<html><body style='color: white; background-color:Black'>";
newBlog.Content += element.Element(contentn + "encoded").Value;
newBlog.Content += "</body></html>";
//
//Extract images from post:
//
// Size the control to fill the form with a margin
var reg = new Regex("src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg|bmp|gif|png))(?:\"|\')?");
var match = reg.Match(newBlog.Content);
if (match.Success)
{
newBlog.FeaturedImage = match.Groups["imgSrc"].Value;
}
else
{
newBlog.FeaturedImage = @"http://karkur.com/no_image.png";
}
BlogPosts.Add(newBlog);
}
}
这就是我得到的结果:
基本上我需要的是在 WP7 中取消转义 unicode 字符:转换:
到普通字符