假定一个天气预报的服务如下:
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Weather : System.Web.Services.WebService
{
public Weather () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string GetWeather(string cityName) {
//根据城市名来返回天气情况
return "Sunny";
}
}
1. 请求的方法 根据协议有Soap 1.1 Soap1.2 HttpPost HttpGet 四种方式
(1)SOAP 1.1
+ ' < 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 > '
+ ' < getWeather xmlns = " http://tempuri.org/ " > '
+ ' < cityName > hangzhou </ cityName > '
+ ' </ getWeather > '
+ ' </ soap:Body > '
+ ' </ soap:Envelope > '
function RequestService() {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite/weather.asmx";
xmlhttp.Open("Post",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("Content-Length",data.length);
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/GetWeather");
xmlhttp.Send(data);
}
(2)SOAP 1.2
+ ' < soap12:Envelope xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " '
+ 'xmlns:xsd = " http://www.w3.org/2001/XMLSchema " xmlns:soap12 = " http://www.w3.org/2003/05/soap-envelope " > '
+ ' < soap12:Body > '
+ ' < getWeather xmlns = " http://tempuri.org/ " > '
+ ' < cityName > hangzhou </ cityName > '
+ ' </ getWeather > '
+ ' </ soap12:Body > '
+ ' </ soap12:Envelope > '
function RequestService() {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite/weather.asmx";
xmlhttp.Open("Post",URL, false);
xmlhttp.SetRequestHeader ("Content-Type"," application/soap+xml; charset=utf-8");
xmlhttp.SetRequestHeader ("Content-Length",data.length);
xmlhttp.Send(data);
}
(3)HttpPost
function RequestService() {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite/weather.asmx/getWeather";
xmlhttp.Open("Post",URL, false);
xmlhttp.SetRequestHeader ("Content-Type"," application/x-www-form-urlencoded");
xmlhttp.SetRequestHeader ("Content-Length",data.length);
xmlhttp.Send(data);
}
(4)HttpGet
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite/weather.asmx/getWeather?cityName=hangzhou";
xmlhttp.Open("GET ",URL, false);
xmlhttp.Send(null);
}
2.中文乱码
webservice接收和传输的消息都是UTF-8编码的,网上有很多解决GB2312传中文参数和接收中文消息乱码的办法,比如用prototype扩展xmlhttp对象,其实也不用那么麻烦,传递参数用escape转换一次,接收时用UTF8Encoding读取出来再根据需要转换即可,解决办法原理差不多.
3.解析返回的Xml DomDocument对象无法使用selectNodes和selectSingleNode
其中一个原因是mozilla不支持此方法,可以通过prototype为它的domdocument对象扩展这两个同名方法,便于用相同的方法处理MS和mozilla兼容问题。但有时就是在IE里也不能正常使用这两个方法,常见情况是取出来的节点值为空。解决办法是在生成的domdocument对象(假设对象为xmlDom)设置一个属性 xmlDom.setProerty("Namespace",'xmlns="na:http://tempuri.org/"')指定一个默认命名空间的前缀,然后在selectNodes和selectSingleNode中使用前缀加标签名如 na:weather就可以了。
4.“意外的以servicemethod/结尾“的错误(类似的格式)
客户端以HttpGet或HttpPost方式调用非本地webservice报“意外的以servicemethod/结尾“的错误,其中一个原因是部署webservice的站点配置中默认是不开启httpget和httppost非本域请求许可的,因此要在web.config补上如下配置
< conformanceWarnings >
< clear />
< add name ="BasicProfile1_1" />
</ conformanceWarnings >
< protocols >
< add name ="HttpSoap1.2" />
< add name ="HttpSoap" />
< add name ="HttpPostLocalhost" />
< add name ="HttpPost" />
< add name ="HttpGet" />
< add name ="Documentation" />
</ protocols >
< soapExtensionTypes >
</ soapExtensionTypes >
< soapExtensionReflectorTypes >
</ soapExtensionReflectorTypes >
< soapExtensionImporterTypes >
</ soapExtensionImporterTypes >
< wsdlHelpGenerator href ="DefaultWsdlHelpGenerator.aspx" />
< serviceDescriptionFormatExtensionTypes >
</ serviceDescriptionFormatExtensionTypes >
</ webServices >
希望打算使用xmlhttp的朋友能够避免这些小问题,也希望大家能够将自己遇到的问题和解决方法继续补全,方便查找也提高自己。上面列举的问题如有更易行的方法,也请指出来,交流一下。