Quiero llamar la google url shortner API de mi C# aplicación de consola, la solicitud de trato de poner en práctica es:llamar a la API de Google Url Shortner en C#
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": " http://www.google.com/ "}
Cuando trato de utilizar este código:
using System.Net;
using System.Net.Http;
using System.IO;
y el método principal es:
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();
}
puedo obtener el código de error 400: Esta API no es compatible con el análisis sintáctico entrada codificada en forma. No sé cómo solucionar esto.
'const cadena MATCH_PATTERN = @" "" id "":? "" (?. +) "" "; Console.WriteLine (Regex.Match (responseText, MATCH_PATTERN) .Groups ["id"]. Value); 'obtiene la URL acortada. –
application/json era la pieza que faltaba para mí. Estaba usando text/json, como un idiota. – Jon