1.用JS访问Web Service:
A.用webservice.htc得到结果,代码如下: B.用XML访问Web Service,得到结果:
<
body MS_POSITIONING
=
"
GridLayout
"
onload
=
"
loadit('5');
"
>
< form id = " Form1 " method = " post " runat = " server " >
< script language = javascript >
function PostRequestData(URL,data)
{
// 感觉不太好用
var xmlhttp = new ActiveXObject( " Microsoft.XMLHTTP " );
xmlhttp.open( " POST " ,URL, false );
xmlhttp.SetRequestHeader( " Content-Type " , " text/xml;charset=utf-8 " );
xmlhttp.SetRequestHeader( " SOAPaction " , " http://tempuri.org/myService/test/isNumner " );
try
{
xmlhttp.Send(data);
var result = xmlhttp.status;
}
catch (ex)
{
return ( " 0 " + ex.description + " | " + ex.number);
}
if (result == 200 )
{
return ( " 1 " + xmlhttp.responseText);
}
xmlhttp = null ;
}
function loadit(value)
{
var url = " http://fenglin.xland.cn " ;
var data;
var r;
data = ' <?xml version="1.0" encoding="utf-8"?> ' ;
data = data + ' <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/envelop/"> ' ;
data = data + " <soap:Body> " ;
data = data + ' <isNumber xmlns="http://tempuri.org/myService/test"> ' ;
data = data + ' <str> ' + value + ' </str> ' ;
data = data + ' </isNumber> ' ;
data = data + ' </soap:Body> ' ;
data = data + ' </soap:Envelope> ' ;
alert(data);
r = PostRequestData(url,data);
document.write(r);
}
</ script >
</ form >
</ body >
< form id = " Form1 " method = " post " runat = " server " >
< script language = javascript >
function PostRequestData(URL,data)
{
// 感觉不太好用
var xmlhttp = new ActiveXObject( " Microsoft.XMLHTTP " );
xmlhttp.open( " POST " ,URL, false );
xmlhttp.SetRequestHeader( " Content-Type " , " text/xml;charset=utf-8 " );
xmlhttp.SetRequestHeader( " SOAPaction " , " http://tempuri.org/myService/test/isNumner " );
try
{
xmlhttp.Send(data);
var result = xmlhttp.status;
}
catch (ex)
{
return ( " 0 " + ex.description + " | " + ex.number);
}
if (result == 200 )
{
return ( " 1 " + xmlhttp.responseText);
}
xmlhttp = null ;
}
function loadit(value)
{
var url = " http://fenglin.xland.cn " ;
var data;
var r;
data = ' <?xml version="1.0" encoding="utf-8"?> ' ;
data = data + ' <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/envelop/"> ' ;
data = data + " <soap:Body> " ;
data = data + ' <isNumber xmlns="http://tempuri.org/myService/test"> ' ;
data = data + ' <str> ' + value + ' </str> ' ;
data = data + ' </isNumber> ' ;
data = data + ' </soap:Body> ' ;
data = data + ' </soap:Envelope> ' ;
alert(data);
r = PostRequestData(url,data);
document.write(r);
}
</ script >
</ form >
</ body >
A. 同步调用(简单):
// 创建 XML Web Service 代理类的实例
localhost1.Service2 s = new example4.localhost1.Service2();
string aa = s.HelloWorld();
B. 异步调用(复杂、快速):
// 创建 XML Web Service 代理类的实例
localhost1.Service2 s = new example4.localhost1.Service2();
// DefaultCredentials 分配给 Web 服务客户端代理的 Credentials 属性
s.Credentials = System.Net.CredentialCache.DefaultCredentials;
// 创建对回调委托的引用
AsyncCallback cb = new AsyncCallback(ServiceCallback);
// 调用begin方法,将回调委托和此代理类实例作为AsyncState对象传递
s.BeginHelloWorld(cb,s);
private void ServiceCallback(IAsyncResult ar)
{
// 将AsyncState对象转换为代理对象
localhost1.Service2 s = (localhost1.Service2)ar.AsyncState;
// 调用end方法并将响应指定数据
try
{
lock ( this )
{
string str = s.EndHelloWorld(ar);
Response.Write(str);
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
// 创建 XML Web Service 代理类的实例
localhost1.Service2 s = new example4.localhost1.Service2();
string aa = s.HelloWorld();
B. 异步调用(复杂、快速):
// 创建 XML Web Service 代理类的实例
localhost1.Service2 s = new example4.localhost1.Service2();
// DefaultCredentials 分配给 Web 服务客户端代理的 Credentials 属性
s.Credentials = System.Net.CredentialCache.DefaultCredentials;
// 创建对回调委托的引用
AsyncCallback cb = new AsyncCallback(ServiceCallback);
// 调用begin方法,将回调委托和此代理类实例作为AsyncState对象传递
s.BeginHelloWorld(cb,s);
private void ServiceCallback(IAsyncResult ar)
{
// 将AsyncState对象转换为代理对象
localhost1.Service2 s = (localhost1.Service2)ar.AsyncState;
// 调用end方法并将响应指定数据
try
{
lock ( this )
{
string str = s.EndHelloWorld(ar);
Response.Write(str);
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
<
script language
=
"
javascript
"
>
<!--
var str_province;
var str_city;
var str_area;
function window_onload()
{
service.useService( " http://localhost/serviceExample1/Service1.asmx?WSDL " , " myselect " );
str_province = service.myselect.callService(province_Result, " getProvince " );
}
function province_Result(result)
{
if ( ! result.error)
{
document.all( " select_province " ).length = 0 ;
if (result.value.substring( 0 , 1 ) == " , " )
result.value = result.value.substring( 1 ,result.length);
var piArray = result.value.split( ' , ' );
for (var i = 0 ;i < piArray.length;i ++ )
{
var ary1 = piArray[i].toString().split( ' | ' );
document.all( " select_province " ).options.add( new Option(ary1[ 1 ].toString(),ary1[ 0 ].toString()));
}
}
}
function province_onchange()
{
var province = document.getElementById( " select_province " );
var pindex = province.selectedIndex;
var pValue = province.options[pindex].value;
var pText = province.options[pindex].text;
str_city = service.myselect.callService(city_Result, " getCity " ,pValue);
}
function city_onchange()
{
var city = document.getElementById( " select_city " );
var cindex = city.selectedIndex;
var cValue = city.options[cindex].value;
var cText = city.options[cindex].text;
str_area = service.myselect.callService(area_Result, " getArea " ,cValue);
}
function city_Result(result)
{
if ( ! result.error)
{
document.all( " select_city " ).length = 0 ;
if (result.value.substring( 0 , 1 ) == " , " )
{
result.value = result.value.substring( 1 ,result.length);
}
var piArray = result.value.split( " , " );
for (var i = 0 ;i < piArray.length;i ++ )
{
var ary1 = piArray[i].toString().split( " | " );
document.all( " select_city " ).options.add( new Option(ary1[ 1 ].toString(),ary1[ 0 ].toString()));
}
}
}
function area_Result(result)
{
if ( ! result.error)
{
document.all( " select_area " ).length = 0 ;
if (result.value.substring( 0 , 1 ) == " , " )
result.value = result.value.substring( 1 ,result.length);
var ciArray = result.value.split( " , " );
for (var i = 0 ;i < ciArray.length;i ++ )
{
var ary1 = ciArray[i].toString().split( " | " );
document.all( " select_area " ).options.add( new Option(ary1[ 1 ].toString(),ary1[ 0 ].toString()));
}
}
}
-->
</ script >
<!--
var str_province;
var str_city;
var str_area;
function window_onload()
{
service.useService( " http://localhost/serviceExample1/Service1.asmx?WSDL " , " myselect " );
str_province = service.myselect.callService(province_Result, " getProvince " );
}
function province_Result(result)
{
if ( ! result.error)
{
document.all( " select_province " ).length = 0 ;
if (result.value.substring( 0 , 1 ) == " , " )
result.value = result.value.substring( 1 ,result.length);
var piArray = result.value.split( ' , ' );
for (var i = 0 ;i < piArray.length;i ++ )
{
var ary1 = piArray[i].toString().split( ' | ' );
document.all( " select_province " ).options.add( new Option(ary1[ 1 ].toString(),ary1[ 0 ].toString()));
}
}
}
function province_onchange()
{
var province = document.getElementById( " select_province " );
var pindex = province.selectedIndex;
var pValue = province.options[pindex].value;
var pText = province.options[pindex].text;
str_city = service.myselect.callService(city_Result, " getCity " ,pValue);
}
function city_onchange()
{
var city = document.getElementById( " select_city " );
var cindex = city.selectedIndex;
var cValue = city.options[cindex].value;
var cText = city.options[cindex].text;
str_area = service.myselect.callService(area_Result, " getArea " ,cValue);
}
function city_Result(result)
{
if ( ! result.error)
{
document.all( " select_city " ).length = 0 ;
if (result.value.substring( 0 , 1 ) == " , " )
{
result.value = result.value.substring( 1 ,result.length);
}
var piArray = result.value.split( " , " );
for (var i = 0 ;i < piArray.length;i ++ )
{
var ary1 = piArray[i].toString().split( " | " );
document.all( " select_city " ).options.add( new Option(ary1[ 1 ].toString(),ary1[ 0 ].toString()));
}
}
}
function area_Result(result)
{
if ( ! result.error)
{
document.all( " select_area " ).length = 0 ;
if (result.value.substring( 0 , 1 ) == " , " )
result.value = result.value.substring( 1 ,result.length);
var ciArray = result.value.split( " , " );
for (var i = 0 ;i < ciArray.length;i ++ )
{
var ary1 = ciArray[i].toString().split( " | " );
document.all( " select_area " ).options.add( new Option(ary1[ 1 ].toString(),ary1[ 0 ].toString()));
}
}
}
-->
</ script >