如何向公司网络上的服务器发出POST请求

时间:2018-05-17 02:48:31

标签: vb.net post

我已获得一些数据来执行ping我们工作网络中包含的服务器的调用。这个调用必须在我建立的vb.net应用程序中进行,但我真的很难开始这个。我尝试过创建一个WebRequest,但我甚至不确定它是否正确。

这里有我所拥有的(" xxx"更换敏感部件):

POST /xxx/XmlService HTTP/1.1
Host: xxx001 (this is just the server name)
Content-Type: text/xml
Cache-Control: no-cache
Postman-Token: xxx (long token)

<?xml version="1.0" ?>
<xxx version="1.0">
        <Request
                Object="System"
                Action="Ping">
        </Request>
</xxx>

我期待回复XML。

有人至少可以指出我正确的方向吗?非常感谢你!

1 个答案:

答案 0 :(得分:0)

我终于把它连接起来了。以下是那些关注的人(使用与OP相同的替换&#34; xxx&#34;字符串):

    Dim uriTest As Uri
    uriTest = New Uri("http://xxx001/xxx/XmlService")


    Dim requestStr As String
    requestStr = Strings.Trim("POST /xxx/XmlService HTTP/1.1" & Environment.NewLine &
                              "Host: xxx001" & Environment.NewLine &
                              "Content-Type: text/xml" & Environment.NewLine &
                              "Cache-Control: no-cache" & Environment.NewLine &
                              "Postman-Token: 6672bfe9-6d44-4770-869d-4f08c24ab143" & Environment.NewLine & Environment.NewLine &
                              "<?xml version=""1.0"" ?>" & Environment.NewLine &
                              "<xxx version=""1.0"">" & Environment.NewLine &
                              "<Request" & Environment.NewLine &
                              "Object=""System""" & Environment.NewLine &
                              "Action=""Ping"">" & Environment.NewLine &
                              "</Request>" & Environment.NewLine &
                              "</xxx>")
    Dim req As HttpWebRequest, webreq As WebRequest
    webreq = WebRequest.Create(uriTest)
    req = CType(webreq, HttpWebRequest)

    req.Method = "POST"

    Dim dataStream As Stream = req.GetRequestStream()
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(requestStr)
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()
    Dim response As WebResponse = req.GetResponse()
    Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
    dataStream = response.GetResponseStream()
    Dim reader As New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    Console.WriteLine(responseFromServer)
    reader.Close()
    dataStream.Close()
    response.Close()