使用VB .NET提交POST表格

问题描述 投票:0回答:1

我已经广泛地研究了我的问题的解决方案,尽管我发现了似乎对其他人有用的答案,但我仍然很难确定这一点。但是我觉得我很亲近。

我正在尝试对名为Zoho Creator的在线应用程序进行Rest API调用。我正在尝试实现“添加记录”调用。他们给出的示例是使用带有提交按钮的HTML表单。但是我需要从VB .NET桌面应用程序添加记录。我已经尝试了WebClient和WebRequest,但是这些尝试都没有成功。但是我已经成功地将这些方法与其他API调用和其他API结合使用,正是这个Add Records给我带来了麻烦。

[必需参数之一是authtoken,出于安全原因,我将其替换为“ xxxxxxxxxx”。这是我创建的html表单,当我使用它时,它通过API成功创建了记录,它只是添加了一条记录,其中“ TicketID”具有单个字段值。

<!DOCTYPE html>
<html>
  <body>
   <form method="POST" action="https://creator.zoho.com/api/max1stdirectcom/xml/service-orders/form/Ticket_form/record/add">
   <input type="hidden" name="authtoken" value="xxxxxxxxxx"/>
   <input type="hidden" name="scope" id="scope" value="creatorapi"/>
   <input type="text" name="TicketID" value="123"/>
   <input type="submit" value="Add Record"/>
</form>
<body>

因此,上面的方法效果很好。现在,这是我的VB .NET代码尝试使用WebRequest复制相同的结果:

Protected Sub PostTo(sTicketID As String)
    Dim url As String = "https://creator.zoho.com/api/max1stdirectcom/xml/service-orders/form/Ticket_form/record/add"
    Dim request As WebRequest = WebRequest.Create(url)

    request.Method = "POST"

    ' Create POST data and convert it to a byte array.  
    Dim postData As String = "?authtoken=" & "xxxxxxxxxx" & "?scope=creatorapi" & "?TicketID=" & sTicketID
    Dim byteArray As Byte() = 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.  
    Dim dataStream As Stream = request.GetRequestStream()

    ' Write the data to the request stream.  
    dataStream.Write(byteArray, 0, byteArray.Length)

    ' Close the Stream object.  
    dataStream.Close()

    ' Get the response.  
    Dim response As WebResponse = request.GetResponse()

    ' Display the status.  
    Debug.WriteLine(CType(response, HttpWebResponse).StatusDescription)

    ' Get the stream containing content returned by the server.  
    dataStream = response.GetResponseStream()

    ' Open the stream using a StreamReader for easy access.  
    Dim reader As New StreamReader(dataStream)

    ' Read the content.  
    Dim responseFromServer As String = reader.ReadToEnd()
    ' Display the content.  

    Debug.WriteLine(responseFromServer)

    ' Clean up the streams.  
    reader.Close()
    dataStream.Close()
    response.Close()

End Sub

我使用上述VB .Net代码从呼叫中获得的响应是​​:

<response>
  <errorlist>
    <error>
      <code>2899</code>
      <message><![CDATA[Permission Denied To Add Record(s).]]></message>
    </error>
  </errorlist>
</response>

因此,它显然在某种程度上与API进行了良好的通信。我使用的是正确的AuthToken,所以不确定为什么它拒绝添加记录。我正在传递与基本表单POST完全相同的“凭据”,但结果有所不同。

有什么建议让我尝试吗?

vb.net post webrequest zoho
1个回答
0
投票

下面是VB .Net中的工作代码

请检查并在代码中添加缺少的实现。

Private Sub HTTPRestPOST (ByVal JsonInputStr As String, ByVal POSTUri As String)

    'Make a request to the POST URI

    Dim RestPOSTRequest As HttpWebRequest = HttpWebRequest.Create(POSTUri)

    'Convert the JSON Input to Bytes through UTF8 Encoding

    Dim JsonEncoding As New UTF8Encoding()

    Dim JsonBytes As Byte() = JsonEncoding.GetBytes(JsonInputStr)

    'Setting the request parameters

    RestPOSTRequest.Method = "POST"

    RestPOSTRequest.ContentType = "application/json"

    RestPOSTRequest.ContentLength = JsonBytes.Length

    'Add any other Headers for the URI

    RestPOSTRequest.Headers.Add("username", "kalyan_nakka")

    RestPOSTRequest.Headers.Add("password", "********")

    RestPOSTRequest.Headers.Add("urikey", "MAIJHDAS54ADAJQA35IJHA784R98AJN")

    'Create the Input Stream for the URI

    Using RestPOSTRequestStream As Stream = RestPOSTRequest.GetRequestStream()

        'Write the Input JSON data into the Stream

        RestPOSTRequestStream.Write(JsonBytes, 0, JsonBytes.Length)

        'Response from the URI

        Dim RestPOSTResponse = RestPOSTRequest.GetResponse()

        'Create Stream for the response

        Using RestPOSTResponseStream As Stream = RestPOSTResponse .GetResponseStream()

            'Create a Reader for the Response Stream

            Using RestPOSTResponseStreamReader As New StreamReader(RestPOSTResponseStream)

                Dim ResponseData = RestPOSTResponseStreamReader.ReadToEnd()

                'Later utilize "ResponseData" variable as per your requirement

                'Close the Reader

                RestPOSTResponseStreamReader.Close()

            End Using

            RestPOSTResponseStream.Close()  

        End Using       

        RestPOSTRequestStream.Close()

    End Using 

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.