【发布时间】:2018-05-02 20:22:50
【问题描述】:
这里尝试调用api是代码
public static void Main()
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("https://creator.zoho.eu/api/programerAnel/xml/testAPP/form/Edit/record/update");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = @"
< input type=""hidden"" name =""authtoken"" value=""12333"">
< input type = ""hidden"" name = ""scope"" id = ""scope"" value = ""creatorapi"">
< input type = ""text"" name = ""criteria"" value = ""UpdateUj=123"" >
< input type = ""text"" name = ""Kljuc"" value = ""1"" >
< input type = ""submit"" value = ""Update Record"" > ";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
但它一直在抛出我的回应。
System.Net.WebException: '远程服务器返回错误: (400) 错误的请求。'
是否可以以这样的格式发送 postData ?
请求发送到Zoho - REST API
【问题讨论】:
-
POST 正文不是
x-www-form-urlencoded。快速搜索如何正确格式化。 -
@Crowcoder 正确的形式是什么意思。在文本下提供的 url 上,他们要求这样的格式
-
在我看来,他们正在展示一个示例 html 表单,该表单将发布到他们的 api,而不是原始的 http 请求详细信息。但是,我可能是错的。如果我是的话,这是一种设计 API 的奇怪方式。
-
@Crowcoder 这就是他们所说的。 Zoho 只能发出简单的 GET 和简单的 POST 请求。 (应用程序/x-www-form-urlencoded)
-
表单 url 编码如下所示:
authtoken=12333&scope=creatorapi&criteria=UpdateUj=123 ... etc。尽管您必须对数据进行编码,尤其是因为数据本身具有=字符。
标签: c# rest httpwebrequest zoho