2

我正在尝试调试一些在我们的 Web 服务中遇到问题的一些 vb 代码。我正在尝试使用 vb .net 提交 XML 文件以尽可能地模仿 VB 代码,因为我没有非 .Net VB IDE。在项目中使用 Web 引用时,我没有遇到此问题。

这是我试图在 VB .Net 中模拟的 VB 代码

Dim oHttp As WinHttp.WinHttpRequest
Set oHttp = New WinHttp.WinHttpRequest


Call oHttp.Open("POST", m_URL, False)
oHttp.setRequestHeader "Content-Type", " text/xml"
Call oHttp.send(request)
Call oHttp.waitForResponse
'Process response
Dim response As String
response = oHttp.responseText
Set oHttp = Nothing

到目前为止,这是我的代码:

    Dim request As WebRequest = WebRequest.Create("http://localhost/service/service.asmx")
    Dim xml As String = String.Empty
    Dim sr As New StreamReader("c:\xmlfile.xml")
    xml = sr.ReadToEnd
    sr.Close()
    sr.Dispose()

    Dim bytesToWrite As Byte() = Encoding.ASCII.GetBytes(xml)

    request.Method = "POST"
    request.ContentLength = bytesToWrite.Length
    request.ContentType = "text/xml"
    request.Headers.Add("SOAPAction", "[web service url]/Submit")

    Dim newStream As Stream = request.GetRequestStream()
    newStream.Write(bytesToWrite, 0, bytesToWrite.Length)
    newStream.Close()

    Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    Dim dataStream As Stream = response.GetResponseStream()
    Dim reader As New StreamReader(dataStream)

    Dim responseFromServer As String = reader.ReadToEnd()

我的 Web 服务正在接收请求,但对象为空

4

0 回答 0