使用VB .NET提交表单POST

时间:2017-08-01 16:11:59

标签: vb.net post webrequest zoho

我已经广泛地搜索了我的问题的解决方案,虽然我找到了似乎对其他人有用的答案,但我真的很难解决这个问题。但我觉得我很亲密。

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

其中一个必需参数是authtoken,出于安全考虑,我将其替换为" xxxxxxxxxx"。这是我创建的一个html表单,当我使用它时,它通过API成功创建了记录,它只是添加了单个字段值为#34; 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,因此不确定为什么拒绝添加记录。我正在通过相同的&#34;凭证&#34;作为POST的基本形式,但得到不同的结果。

我有什么建议可以尝试吗?

1 个答案:

答案 0 :(得分: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