Function HttpPost(Url As String, PostMsg As String) As String
'自己寫的發送Post的函式
'函式回傳值是獲得的回傳資訊(HTML)
'第一個引數是要發送的Url地址
'第二個引數是要發送的訊息(鍵值對應,不必編碼)
Dim XmlHttp As Object
Set XmlHttp = CreateObject("Msxml2.XMLHTTP")
If Not IsObject(XmlHttp) Then
Set XmlHttp = CreateObject("Microsoft.XMLHTTP")
If Not IsObject(XmlHttp) Then Exit Function
End If
XmlHttp.open "POST", Url, False, Null, Null
XmlHttp.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
XmlHttp.send (PostMsg)
Do While XmlHttp.readyState <> 4
DoEvents
Loop
'如果把下面一行(以及后面的End IF)的注釋去除,即設定為僅當回傳碼是200時才回傳頁面內容
'If XmlHttp.Status = 200 Then
HttpPost = XmlHttp.responseText
'End If
End Function
'直接把這個函式放進表單/模塊里就行,不必參考任何東西
Private Sub Form_Load()
Dim PostMsg As String
On Error Resume Next
'PostMsg = "<?xmlPOST /WebServices/WeatherWebService.asmx HTTP/1.1"
'PostMsg = PostMsg & "Host:www.webxml.com.cn"
'PostMsg = PostMsg & "Content-Type: text/xml; charset=utf-8"
'PostMsg = PostMsg & "Content -length: 2000"
'PostMsg = PostMsg & "SOAPAction:""http://WebXml.com.cn/getWeatherbyCityName"""
'PostMsg = PostMsg & "<?xml version=""1.0"" encoding=""utf-8""?>"
'PostMsg = PostMsg & "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
'PostMsg = PostMsg & "<soap:Body>"
'PostMsg = PostMsg & "<getWeatherbyCityName xmlns=""http://WebXml.com.cn"">"
'PostMsg = PostMsg & "<theCityName>石家莊</theCityName>"
'PostMsg = PostMsg & "</getWeatherbyCityName>"
'PostMsg = PostMsg & "</soap:Body>"
'PostMsg = PostMsg & "</soap:Envelope/?>"
PostMsg = "<?xmlPOST /WebServices/WeatherWebService.asmx/getWeatherbyCityName HTTP/1.1"
PostMsg = PostMsg & "Host: www.webxml.com.cn"
PostMsg = PostMsg & "Content-Type: application/x-www-form-urlencoded"
PostMsg = PostMsg & "Content -length: 1000"
PostMsg = PostMsg & "theCityName=石家莊?>"
RichTextBox1.Text = HttpPost("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName", PostMsg)
Text1.Text = PostMsg
End Sub
uj5u.com熱心網友回復:
POST /WebServices/WeatherWebService.asmx HTTP/1.1Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getWeatherbyCityName"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getWeatherbyCityName xmlns="http://WebXml.com.cn/">
<theCityName>string</theCityName>
</getWeatherbyCityName>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/">
<getWeatherbyCityNameResult>
<string>string</string>
<string>string</string>
</getWeatherbyCityNameResult>
</getWeatherbyCityNameResponse>
</soap:Body>
</soap:Envelope>
這是那個天氣網站給的實體,演示。我就是按照這個步驟。
uj5u.com熱心網友回復:
提示這個,<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="zh-CHS">服務器無法處理請求。 ---> 缺少根元素。</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
uj5u.com熱心網友回復:
它的“實體,演示”的網頁URL貼出來,讓大家直接去看。
你這樣“斷章取義”的Copy 一堆過來,鬼才知道你這用得是否正確。
uj5u.com熱心網友回復:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmxuj5u.com熱心網友回復:
另一個帖子,已經給你解決問題了。經試驗,使用上根本用不著你上面那么繁雜,簡單幾句代碼就行了。
uj5u.com熱心網友回復:
呼叫web service何必這么費勁,用現成的soap toolkit 就可以了。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/45278.html
標籤:API
