Web Services 如何使用HttpWebRequest调用Web服务

ltskdhd1  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(235)

我能够用下面的代码成功地从服务器检索wsdl。
现在如何从同一个服务中调用方法(GetVersion)?
尝试http://www.servername.com/DataService.asmx/GetVersion返回一个错误,说找不到该页。我从.NET WebService引用调用该方法没有问题,但我希望能够使用HttpWebRequest

Dim req As HttpWebRequest
Dim resp As HttpWebResponse
Dim sr As IO.StreamReader

req = WebRequest.Create("http://www.servername.com/DataService.asmx?wsdl")
resp = req.GetResponse

sr = New IO.StreamReader(resp.GetResponseStream)
txt.Text = sr.ReadToEnd
sr.Close()
ojsjcaue

ojsjcaue1#

我相信您唯一的问题是Web服务器的配置。每个Web服务的配置(在我的例子中是web.config文件,因为我使用的是IIS)应该允许管理允许访问它的协议:获取、发布和/或Soap。
我刚刚测试了我自己的Web服务,它对来自我的浏览器的GET请求做出了响应......它还对POST请求做出了预期的响应(我将https://web.archive.org/web/20210619192654/https://www.4guysfromrolla.com/articles/022410-1.aspx中的一些代码改编为以下代码片段:)

Dim payload As Byte()
    payload = Text.Encoding.ASCII.GetBytes("paramName=firstValue&p2=secondValue")

    Dim webRequest As System.Net.HttpWebRequest
    webRequest = System.Net.HttpWebRequest.Create("http://www.servername.com/DataService.asmx/GetVersion")
    webRequest.Method = "POST"
    webRequest.KeepAlive = False
    webRequest.ContentType = "application/x-www-form-urlencoded"
    webRequest.ContentLength = payload.Length

    Dim reqStream As System.IO.Stream
    reqStream = webRequest.GetRequestStream()
    reqStream.Write(payload, 0, payload.Length)
    reqStream.Close()

    Dim webResponse As System.Net.HttpWebResponse
    webResponse = webRequest.GetResponse()

    Dim reader As System.IO.StreamReader
    reader = New System.IO.StreamReader(webResponse.GetResponseStream())
    Dim xmlDoc As System.Xml.XmlDocument
    xmlDoc = New System.Xml.XmlDocument()
    xmlDoc.LoadXml(reader.ReadToEnd())

祝你好运

相关问题