Я хочу вызвать API сокращения URL-адресов Google из моего консольного приложения C # , я пытаюсь реализовать следующий запрос:
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: 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("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 не поддерживает синтаксический анализ ввода с кодировкой формы. Я не знаю, как это исправить.