C# 具有输入类型字段的Web请求
在这里尝试进行api调用是一种代码C# 具有输入类型字段的Web请求,c#,rest,httpwebrequest,zoho,C#,Rest,Httpwebrequest,Zoho,在这里尝试进行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");
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();
}
publicstaticvoidmain()
{
//使用可以接收帖子的URL创建请求。
WebRequest=WebRequest.Create(“https://creator.zoho.eu/api/programerAnel/xml/testAPP/form/Edit/record/update");
//将请求的Method属性设置为POST。
request.Method=“POST”;
//创建POST数据并将其转换为字节数组。
字符串postData=@”
”;
byte[]byteArray=Encoding.UTF8.GetBytes(postData);
//设置WebRequest的ContentType属性。
request.ContentType=“application/x-www-form-urlencoded”;
//设置WebRequest的ContentLength属性。
request.ContentLength=byteArray.Length;
//获取请求流。
Stream dataStream=request.GetRequestStream();
//将数据写入请求流。
写入(byteArray,0,byteArray.Length);
//关闭流对象。
dataStream.Close();
//得到回应。
WebResponse=request.GetResponse();
//显示状态。
Console.WriteLine(((HttpWebResponse)response.StatusDescription);
//获取包含服务器返回的内容的流。
dataStream=response.GetResponseStream();
//使用StreamReader打开流以便于访问。
StreamReader=新的StreamReader(数据流);
//阅读内容。
字符串responseFromServer=reader.ReadToEnd();
//显示内容。
Console.WriteLine(responseFromServer);
//清理溪流。
reader.Close();
dataStream.Close();
response.Close();
}
但它一直在给我回应
System.Net.WebException:'远程服务器返回错误:(400)
请求不好。”
有可能以这样的格式发送postData吗
请求被发送到我还不知道您可能还有什么其他问题,但首先请将您的帖子正文正确格式化为
application/x-www-form-urlencoded
。您发送的是HTML,我不相信这是他们文档的意图。它们向您展示了如何构建HTML表单,而不是如何从代码中调用API。如果您在网页上有这样一个表单,它会将其发布到API:
authtoken=12333&scope=creatorapi&criteria=UpdateUj%3d123&Kljuc=1
注意,我用
%3d
对=
字符进行了编码。您应该UrlEncode
确保所有数据的安全。我还不知道您可能还有什么其他问题,但首先请将您的帖子正文正确格式化为application/x-www-form-urlencoded
。您发送的是HTML,我不相信这是他们文档的意图。它们向您展示了如何构建HTML表单,而不是如何从代码中调用API。如果您在网页上有这样一个表单,它会将其发布到API:
authtoken=12333&scope=creatorapi&criteria=UpdateUj%3d123&Kljuc=1
注意,我用
%3d
对=
字符进行了编码。您应该UrlEncode
确保所有数据的安全。该帖子正文不是x-www-form-urlencoded
。快速搜索如何正确设置格式。@Crowcoder正确的格式是什么意思。在文本下提供的url上,他们请求这样的格式。在我看来,他们显示的是一个示例html表单,将发布到他们的api,而不是原始http请求详细信息。但是,我可能错了。如果我是,设计API是一种奇怪的方式。@Crowcoder他们就是这么说的。Zoho只能发出简单的GET和POST请求。(application/x-www-form-urlencoded)表单url编码如下:authtoken=12333&scope=creatorapi&criteria=UpdateUj=123。。。etc
。尽管您必须对数据进行编码,尤其是因为数据本身具有=
字符。该帖子正文不是x-www-form-urlencoded
。快速搜索如何正确设置格式。@Crowcoder正确的格式是什么意思。在文本下提供的url上,他们请求这样的格式。在我看来,他们显示的是一个示例html表单,将发布到他们的api,而不是原始http请求详细信息。但是,我可能错了。如果我是,设计API是一种奇怪的方式。@Crowcoder他们就是这么说的。Zoho只能发出简单的GET和POST请求。(application/x-www-form-urlencoded)表单url编码如下:authtoken=12333&scope=creatorapi&criteria=UpdateUj=123。。。etc
。尽管您必须对数据进行编码,尤其是因为数据本身具有=
字符。