我想從我的C#控制檯應用程序調用google url shortner API,我嘗試實現請求是:調用C#谷歌網址Shortner API
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<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();
}
我得到的錯誤代碼400:此API不支持解析表單編碼輸入。 我不知道如何解決這個問題。
'const string MATCH_PATTERN = @「」「id」「:?」「(?。+)」「」; Console.WriteLine(Regex.Match(responseText,MATCH_PATTERN).Groups [「id」]。Value);'獲取縮短的URL。 –
應用程序/ json對我來說是缺失的部分。正在使用文本/ JSON,就像一個白癡。 – Jon