早上好, 我一直在寻找我的问题的答案,但我没有找到任何东西。 我需要对Webapi进行REST调用,这是我与cURL一起使用的代码:
curl -X POST --include 'https://animetrics.p.mashape.com/detect?api_key=sample' \
-H 'X-Mashape-Key: sample' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json' \
-d 'selector=FACE, EYES, FULL' \
-d 'url=http://example.com/some_image.jpg'
我已经能够在C#中编写以下代码:
public string MakeRequest(string parameters)
{
var request = (HttpWebRequest)WebRequest.Create(EndPoint);
request.Method = Method.ToString();
request.ContentLength = 0;
request.ContentType = ContentType;
request.Headers["X-Mashape-Key"] = "sample";
request.Accept = "application/json";
PostData += "selector=FACE&";
PostData += "url=" +HttpUtility.UrlEncode("http://www.sample.it/sample.jpg");
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)
{
var bytes= Encoding.ASCII.GetBytes(PostData);
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
// grab the response
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
但作为回应,我总是得到一个json对象,里面说:
"{\"errors\":{\"url\":\"url or image field required\"}}"
有人可以给我一些帮助吗?
谢谢
更新 问题解决了我在Content-Type字段中错过了最后一个d。 谢谢大家!
答案 0 :(得分:0)
更新:问题已解决我在Content-Type字段中错过了最后一个d,并且没有从调试器发出错误信号。谢谢你们! 米歇尔