在 C# 中调用 google Url Shortner API
我想从我的 C# 控制台应用程序调用 google 网址缩短 API ,我尝试实现的请求是:
发布https://www.googleapis.com/urlshortener/v1/url
内容类型: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不支持解析表单编码输入。 我不知道如何解决这个问题。
I want to call the google url shortner API 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以检查下面的代码(使用System.Net)。
您应该注意到,必须指定 contenttype,并且必须是“application/json”;并且要发送的字符串必须是 json 格式。
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.
Google 有一个用于使用 Urlshortener API 的 NuGet 包。您可以在此处找到详细信息。
基于此示例 您可以这样实现它:
如果您位于防火墙后面,您可能需要使用代理。下面是 ProxySupportedHttpClientFactory 的实现,在上面的示例中已被注释掉。这归功于 这篇博文。
Google has a NuGet package for using the Urlshortener API. Details can be found here.
Based on this example you would implement it as such:
If you are behind a firewall you may need to use a proxy. Below is an implementation of the
ProxySupportedHttpClientFactory
, which is commented out in the sample above. Credit for this goes to this blog post.怎么样
改成
How about changing
to
以下是我的工作代码。可能对你有帮助。
Following is my working code. May be its helpful for you.