voglio chiamare il google url shortner API dal mio C# applicazione console, la richiesta cerco di implementare è:chiamando API Google URL Shortner in C#
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": " http://www.google.com/ "}
Quando provo ad usare questo codice:
using System.Net;
using System.Net.Http;
using System.IO;
e il metodo principale è:
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();
}
ottengo il codice di errore 400: Questa API non supporta l'analisi input codificato in forma. Non so come risolvere questo problema.
'const string MATCH_PATTERN = @" "" id "":? "" (?. +) "" "; Console.WriteLine (Regex.Match (responseText, MATCH_PATTERN) .Groups ["id"]. Value); 'ottiene l'URL di riduzione. –
application/json era il pezzo mancante per me. Stavo usando testo/JSON, come un idiota. – Jon