Aufruf google-Url Shortner-API in C#

Möchte ich nennen, die google url shortner-API aus meinem C# - Konsolenanwendung, die Anfrage, die ich versuche zu implementieren ist:

POST https://www.googleapis.com/urlshortener/v1/url

Content-Type: application/json

{"longUrl": "http://www.google.com/"}

Wenn ich versuche, um diesen code zu verwenden:

using System.Net;
using System.Net.Http;
using System.IO;

und die main-Methode ist:

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();
}

Bekomme ich den Fehlercode 400: Diese API nicht unterstützt parsing-form-encoded input.
Ich weiß nicht, wie dieses Problem zu beheben.

InformationsquelleAutor smohamed | 2011-11-06

Schreibe einen Kommentar