1
我試圖打電話http://genderize.io/,但我從.NET說得到一個錯誤:呼叫Genderize.io API從C#
{"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;
}
即使我用得到,{「內容長度或分塊編碼不能被設置爲不寫數據的操作「} – Ramie
你會想擺脫'ContentType'和'ContentLength'行 – HackedByChinese
由於你正在做一個GET請求,你不必指定request.ContentLength = postData.Length; – enrique7mc