【问题标题】:Web request with input type fields带有输入类型字段的 Web 请求
【发布时间】: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&amp;scope=creatorapi&amp;criteria=UpdateUj=123 ... etc。尽管您必须对数据进行编码,尤其是因为数据本身具有 = 字符。

标签: c# rest httpwebrequest zoho


【解决方案1】:

我还不知道您可能还有什么其他问题,但请先将您的帖子正文正确格式化为application/x-www-form-urlencoded。您发送的是 HTML,我不认为这是他们文档的意图。他们向您展示了如何构建 HTML 表单,而不是如何从代码中访问 API。如果您在网页上有类似的表单,它会将其发布到 API:

authtoken=12333&scope=creatorapi&criteria=UpdateUj%3d123&Kljuc=1

注意我用%3d 编码了= 字符。你应该UrlEncode你所有的数据是安全的。

【讨论】:

    猜你喜欢
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2020-12-17
    • 2021-07-26
    • 2017-11-02
    • 2011-04-17
    相关资源
    最近更新 更多