我尝试了以下方法来检索 Web 服务的结果:
<%
Dim oRequest
Set oRequest = Server.CreateObject("MSXML2.ServerXMLHTTP")
oRequest.setOption(2) = SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
oRequest.setTimeouts 10000, 10000, 10000, 10000
msURL = "http://[0.0.0.0]/contale/customer.asmx/GetInfoFromWhitesPages"
msSOAP = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
msSOAP = msSOAP & "<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">"
msSOAP = msSOAP & "<s:Body>"
msSOAP = msSOAP & "<GetInfoFromWhitesPagesResponse xmlns=""http://[0.0.0.0]/Contale/"">"
msSOAP = msSOAP & "<phone>4504656253</phone>"
msSOAP = msSOAP & "</GetInfoFromWhitesPagesResponse>"
msSOAP = msSOAP & "</s:Body>"
msSOAP = msSOAP & "</s:Envelope>"
oRequest.Open "POST", msURL, False
oRequest.setRequestHeader "Content-Type", "text/xml"
oRequest.setRequestHeader "SOAPAction", "[Some Url]"
oRequest.send msSOAP
Response.Write oRequest.ResponseBody
%>
但结果显示有:??????????????????4??????????????????????????? †????????????????????????????????????†?4???????????? ????????????????????????
我还尝试了以下方法:
<html>
<head><title></title></head>
<body>
<%
Dim objHTTP, strEnvelope
Set objHTTP = Server.CreateObject("Microsoft.XMLHTTP")
strEnvelope = "<?xml version='1.0' encoding='utf-8'?>"
strEnvelope = strEnvelope & "<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/'>"
strEnvelope = strEnvelope & "<soap:Body>"
strEnvelope = strEnvelope & "<GetInfoFromWhitesPagesResponse xmlns='http://[0.0.0.0]/Contale'>"
strEnvelope = strEnvelope & "<phone>4504656253</phone>"
strEnvelope = strEnvelope & "</GetInfoFromWhitesPagesResponse>"
strEnvelope = strEnvelope & "</soap:Body></soap:Envelope>"
Dim url
url = "http://[0.0.0.0]/contale/customer.asmx"
With objHTTP
.Open "post", url, False
.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
.setRequestHeader "SOAPAction", "http://[0.0.0.0]/contale/GetInfoFromWhitesPagesResponse"
.send strEnvelope
End With
Dim strResponse
strResponse = objHTTP.responseXML.Text
If (strResponse = "") Then
Response.Write("Invalid user")
Else
Response.Write(strResponse)
End If
%>
</body>
</html>
但是,我得到了这个:
soap:ClientSystem.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://[0.0.0.0]/contale/GetInfoFromWhitesPagesResponse. at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
这是我的 WebService 的代码:
using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Web.Services;
namespace CustomersInfo
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://[0.0.0.0]/Contale/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public CustomerData GetInfoFromWhitesPages(string phone)
{
var customer = new CustomerData();
var webClient = new WebClient();
webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 Other");
var content = webClient.DownloadString("http://www.whitepages.com/phone/" + phone);
const string infoStart = "<div class=\"address-card\">";
if (content.Contains(infoStart))
{
var startPos = content.IndexOf("<div class=\"address-card\">", StringComparison.Ordinal) + infoStart.Length;
var endPos = content.IndexOf("</div>", startPos, StringComparison.Ordinal);
var info = content.Substring(startPos, endPos - startPos).TrimEnd().TrimStart();
info = info.Replace("\n", "").Replace("<p>", "").Replace("</p>", "");
string[] tabInfo = Regex.Split(info, "<br />");
string name = tabInfo[0];
string address = tabInfo[2];
string city = tabInfo[3].Split(' ')[0].Replace(",", "");
string province = tabInfo[3].Split(' ')[1];
string zip = tabInfo[3].Split(' ')[2] + " " + tabInfo[3].Split(' ')[3];
customer.Name = name;
customer.Address = address;
customer.City = city;
customer.Province = province;
customer.Zip = zip;
}
return customer;
}
public struct CustomerData
{
public string Name;
public string Address;
public string City;
public string Province;
public string Zip;
}
}
}
任何帮助将不胜感激。
谢谢