How to call a GOOGLE API in C#?

calling google Url Shortner API in C#

  • I want to call the http://code.google.com/apis/urlshortener/v1/getting_started.html from my C# Console Application, the request I try to implement is: POST https://www.googleapis.com/urlshortener/v1/url Content-Type: application/json {"longUrl": "http://www.google.com/"} When I try to use this code: using System.Net; using System.Net.Http; using System.IO; and the main method is: 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(); } I get the error code 400: This API does not support parsing form-encoded input. I don't know how to fix this.

  • Answer:

    you can check the code below (made use of System.Net). You should notice that the contenttype must be specfied, and must be "application/json"; and also the string to be send must be in json format. using System; using System.Net; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url"); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = "{\"longUrl\":\"http://www.google.com/\"}"; Console.WriteLine(json); streamWriter.Write(json); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var responseText = streamReader.ReadToEnd(); Console.WriteLine(responseText); } } } }

Sherif Maher Eaid at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

How about changing var requestContent = new FormUrlEncodedContent(new[] {new KeyValuePair<string, string>("longUrl", s),}); to var requestContent = new StringContent("{\"longUrl\": \" + s + \"}");

competent_tech

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.