通过在VB.Net中模拟客户端来调试Web服务

时间:2016-03-31 14:40:56

标签: vb.net web-services

我一生都没有使用VB.Net,但我需要在语言中创建一段代码用于测试目的。

我们为各种客户提供Web服务,这些客户以所需语言构建应用程序,但以XML格式将数据发布到服务中。其中一个客户端一直在弄错,并坚持认为问题就在我们的最后,因此我想模拟XML帖子以证明他们的实现存在问题,这在VB.Net中。

我已经搜索了一段代码并在Visual Studio 2015上进行了设置,但无法让它运行。

Imports System.Net
Imports System.Text
......
......    

        Dim myxml As String
        myxml = "<?xml version='1.0' encoding='utf-8'?>"
        myxml += "<ARBCreateSubscriptionRequest xmlns='AnetApi/xml/v1/schema/AnetApiSchema.xsd'>"
        myxml += "<merchantAuthentication>"
        myxml += "<name>xxx</name>"
        myxml += "<transactionKey>yyy</transactionKey>"
        myxml += "</merchantAuthentication>"
        myxml += "<refId>Sample</refId>"
        myxml += "<subscription>"
        myxml += "<name>Sample subscription</name>"
        myxml += "<paymentSchedule>"
        myxml += "<interval>"
        myxml += "<length>1</length>"
        myxml += "<unit>months</unit>"
        myxml += "</interval>"
        myxml += "<startDate>2010-03-15</startDate>"
        myxml += "<totalOccurrences>12</totalOccurrences>"
        myxml += "<trialOccurrences>1</trialOccurrences>"
        myxml += "</paymentSchedule>"
        myxml += "<amount>10.29</amount>"
        myxml += "<trialAmount>0.00</trialAmount>"
        myxml += "<payment>"
        myxml += "<creditCard>"
        myxml += "<cardNumber>4111111111111111</cardNumber>"
        myxml += "<expirationDate>2010-08</expirationDate>"
        myxml += "</creditCard>"
        myxml += "</payment>"
        myxml += "<billTo>"
        myxml += "<firstName>John</firstName>"
        myxml += "<lastName>Smith</lastName>"
        myxml += "</billTo>"
        myxml += "</subscription>"
        myxml += "</ARBCreateSubscriptionRequest>"


        Try
            Dim bytes As Byte() = Encoding.UTF8.GetBytes(myxml)
            Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://apitest.authorize.net/xml/v1/request.api"), HttpWebRequest)
            request.Method = "POST"
            request.ContentLength = bytes.Length
            request.ContentType = "text/xml"
            Using requestStream As Stream = request.GetRequestStream()
                requestStream.Write(bytes, 0, bytes.Length)
            End Using
            Dim ReceiveStream As Stream
            Dim encode As Encoding
            Dim sr As StreamReader
            Using myresponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
                If Response.StatusCode <> HttpStatusCode.OK Then
                    Dim message As String = [String].Format("POST failed. Received HTTP {0}", Response.StatusCode)
                    Throw New ApplicationException(message)
                End If
                ReceiveStream = myresponse.GetResponseStream()
                encode = System.Text.Encoding.GetEncoding("utf-8")
                sr = New StreamReader(ReceiveStream)
                Response.Write(sr.ReadToEnd)
            End Using






        Catch Exc As Exception


        Finally


        End Try

    End Sub

我无法编译要运行的代码,因为Visual Studio指出了以下问题:

  • &#39;的ContentLength&#39;不是&#39; HttpWebRequest&#39;
  • 的成员
  • &#39; GetRequestStream&#39;不是&#39; HttpWebRequest&#39;
  • 的成员
  • &#39;的GetResponse&#39;不是&#39; HttpWebRequest&#39;
  • 的成员
  • &#39;响应&#39;没有声明,可以看到这个,但不知道它属于哪个命名空间

对此的任何帮助都将受到高度赞赏

0 个答案:

没有答案