【发布时间】:2015-02-15 12:43:01
【问题描述】:
我正在制作一个使用 memes 的 c# asp.net 网站。我正在使用 HttpClient.PostAsync(url, FormUrlEncodedContent) 对 imgflip.com 进行 API 调用 - 这将返回一个 JSON 对象,其中包含指向其服务器上生成的 meme 的链接。在 API 文档中,成功的结果如下所示:
{
"success": true,
"data": {
"url": "http://i.imgflip.com/123abc.jpg",
"page_url": "https://imgflip.com/i/123abc"
}
}
我得到的结果看起来像这样(注意 url 中的正斜杠 和 反斜杠):
{"success":true,"data":{"url":"http:\/\/i.imgflip.com\/ho1uk.jpg","page_url":"https:\/\/imgflip.com\/i\/ho1uk"}}
我需要解析出反斜杠并且 url 工作正常 - 为什么我首先要得到它们?将它们解析出来很简单,但它们存在的事实让我认为我的请求一定有问题。这是我用来获取响应的方法:
public async Task<string> GetReponseAsString(string templateID, string userName, string password, string topText, string bottomText) //non-optional parameters
{
string postURL = "https://api.imgflip.com/caption_image";
var formContent = new FormUrlEncodedContent(new[]{
new KeyValuePair<string, string>("template_id", templateID),
new KeyValuePair<string, string>("username", userName),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("text0", topText),
new KeyValuePair<string, string>("text1", bottomText)
});
HttpClient client = new HttpClient();
var response = await client.PostAsync(postURL, formContent);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return response.Content.ReadAsStringAsync().Result;
}
我得到了正确的响应(几乎)并在内容标题类型中指定了正确的内容类型 - 任何想法为什么我返回的对象有反斜杠?
【问题讨论】: