我正在尝试使用WebRequest调用api,Api方法的帖子是我的代码。
string ContactUs = "https://nestiolistings.com/api/v1/clients/" + APIKey;
var request = (HttpWebRequest)WebRequest.Create(ContactUs);
request.Headers.Add("Authorization", "Basic ############");
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
JavaScriptSerializer jss = new JavaScriptSerializer();
List<people> list = new List<people>();
people obj = new people();
obj.first_name = model.Name;
obj.last_name = model.Name;
obj.email = model.Email;
obj.phone_1 = "";
obj.date_of_birth = "";
list.Add(obj);
RequestModel rm = new RequestModel();
rm.people = list;
rm.notes = model.Message;
// string yourdata = jss.Deserialize<UserInputParameters>(stdObj);
string yourdata = jss.Serialize(rm);
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
requestWriter.Write(yourdata);
requestWriter.Close();
StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
request.GetResponse().Close();
但它给了所有时间404找不到错误。请给一些人帮忙。
答案 0 :(得分:1)
string ContactUs = "https://nestiolistings.com/api/v1/clients/" + APIKey;
我认为您不应该将API密钥附加到网址。假设您的密钥为12345
,您最终会得到https://nestiolistings.com/api/v1/clients/12345
之类的网址。
它不会映射到API中的有效资源 - &#34;客户端&#34;方法是用于创建客户端,因此它不接受特定的客户端ID作为额外的路由参数(因为客户端不存在但因此没有ID),即使它没有从逻辑上讲,这样做,显然,您的API密钥与有效的客户端ID不匹配。
应该在授权标题中提供API密钥。
所以:
string ContactUs = "https://nestiolistings.com/api/v1/clients/";
var request = (HttpWebRequest)WebRequest.Create(ContactUs);
request.Headers.Add("Authorization", "Basic " + APIKey);
(这假设APIKey
已经是API所要求的base64编码字符串。)
有关详细信息,请参阅http://developers.nestio.com/api/v1/clients.html和http://developers.nestio.com/api/v1/auth.html。