Genderize打电话.io API来自c#
本文关键字:来自 API io 打电话 Genderize | 更新日期: 2023-09-27 18:03:32
我试图调用http://genderize.io/,但我得到一个错误从。net说:
{"You must provide a request body if you set ContentLength>0 or SendChunked==true. Do this by calling [Begin]GetRequestStream before [Begin]GetResponse."}
如何调用这个web服务"http://api.genderize.io/"?name=peter"从c#和得到JSON字符串回来吗?
HttpWebRequest request;
string postData = "name=peter"
URL = "http://api.genderize.io/?"
Uri uri = new Uri(URL + postData);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
request.AllowAutoRedirect = true;
UTF8Encoding enc = new UTF8Encoding();
string result = string.Empty;
HttpWebResponse Response;
try
{
using (Response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = Response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
{
return readStream.ReadToEnd();
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
throw ex;
}
您正在使用POST方法调用服务,通过阅读http://genderize.io/中的注释区域,作者声明只允许GET方法请求。
Stroemgren: Yes, this is confirmed. Only HTTP GET request are allowed.
这个答案作为评论可能会更好,但我没有足够的声誉:(