【发布时间】:2011-11-06 20:09:22
【问题描述】:
我想从我的 C# 控制台应用程序中调用 google url shortner API,我尝试实现的请求是:
发布https://www.googleapis.com/urlshortener/v1/url
内容类型:application/json
{"longUrl": "http://www.google.com/"}
当我尝试使用此代码时:
using System.Net;
using System.Net.Http;
using System.IO;
主要方法是:
static void Main(string[] args)
{
string s = "http://www.google.com/";
var client = new HttpClient();
// Create the HttpContent for the form to be posted.
var requestContent = new FormUrlEncodedContent(new[] {new KeyValuePair<string, string>("longUrl", s),});
// Get the response.
HttpResponseMessage response = client.Post("https://www.googleapis.com/urlshortener/v1/url",requestContent);
// Get the response content.
HttpContent responseContent = response.Content;
// Get the stream of the content.
using (var reader = new StreamReader(responseContent.ContentReadStream))
{
// Write the output.
s = reader.ReadToEnd();
Console.WriteLine(s);
}
Console.Read();
}
我收到错误代码 400:此 API 不支持解析表单编码输入。 我不知道如何解决这个问题。
【问题讨论】:
标签: c# .net httpwebrequest google-api