将XML文件发布到Web服务而不使用服务引用

时间:2012-08-23 14:57:46

标签: xml vb.net web-services post soap

我正在尝试调试一些遇到我们的某个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()

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

0 个答案:

没有答案