flash9/as3访问WebService的暂时替代方法
作者:roading 日期:2007-07-03 English
问题已经解决,实现方法请看(flash9/flash cs3(as3)通过soap访问Web Services)
在前面写了flash9/as3访问WebService的的方法,后来发现这种方法不可行,一旦ws和flash不在同一机器上,flash就不能直接post ws 的方法.
google了一下,发现很多人也在寻找flash9/as3访问WebService的方法(http://www.twelvestone.com/forum_thread/view/35257),搜索了asp和php调用ws的方法, 通常可以使用post/soap的方法访问 WebService,其中使用post的请求方式和我前面介绍的方法一样,可惜使用flash就不行.
那么在没有找到好的方法使用ws之前,能不能用flash9/as3访问WebService呢,我觉得目前有两种替代的方法.
1.用asp,php,asp.net等等来访问ws,然后再用flash去调用这些页面,当然这种方法太不可取了...
2.在同一页面使用两个swf,有一不可见的as2的swf,另外的是as3的要使用的swf,使用as2连接ws,然后as3的swf利用localConnection获取as2访问ws得到的结果...
其实最根本的方法还是直接寻找as3连接ws的方法.
1.改写flash8里面的as2的mx.services包.
2.找到flex的rpc包的源文件
3.寻找另外或者跟简单的方法(flash remoting就可以很方便的利用as3访问,ws也许也有).
//
下面是利用asp和php访问ws的方法,没有利用封装的内容访问,也许能有参考作用(内容摘自论坛,不知道原始地址是哪儿,如果有朋友知道请留言)
//
asp的方法
//
1. soap请求方式
2. post请求方式
---------------------
一.soap请求示例
下面是一个 soap 请求示例。所显示的占位符需要由实际值替换。
post /webservice1/usersignon.asmx http/1.1
host: 192.100.100.81
content-type: text/xml; charset=utf-8
content-length: length
soapaction: "http://tempuri.org/loginbyaccount"
<?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>
<loginbyaccount xmlns="http://tempuri.org/">
<username>string</username>
<password>string</password>
</loginbyaccount>
</soap:body>
</soap:envelope>
为了与webservice交互,需要构造一个与上完全相同的soap请求:
<%
url = "http://192.100.100.81/webservice1/usersignon.asmx"
soaprequest="<?xml version="&chr(34)&"1.0"&chr(34)&" encoding="&chr(34)&"utf-8"&chr(34)&"?>"& _
"<soap:envelope xmlns:xsi="&chr(34)&"http://www.w3.org/2001/xmlschema-instance"&chr(34)&" "& _
"xmlns:xsd="&chr(34)&"http://www.w3.org/2001/xmlschema"&chr(34)&" "& _
"xmlns:soap="&chr(34)&"http://schemas.xmlsoap.org/soap/envelope/"&chr(34)&">"& _
"<soap:body>"& _
"<loginbyaccount xmlns="&chr(34)&"http://tempuri.org/"&chr(34)&">"& _
"<username>"&username&"</username>"& _
"<password>"&password&"</password>"& _
"</loginbyaccount>"& _
"</soap:body>"& _
"</soap:envelope>"
set xmlhttp = server.createobject("msxml2.xmlhttp")
xmlhttp.open "post",url,false
xmlhttp.setrequestheader "content-type", "text/xml;charset=utf-8"
xmlhttp.setrequestheader "host","192.100.100.81"
xmlhttp.setrequestheader "content-length",len(soaprequest)
xmlhttp.setrequestheader "soapaction", "http://tempuri.org/loginbyaccount" ‘一定要与webservice的命名空间相同,否则服务会拒绝
xmlhttp.send(soaprequest)
‘这样就利用xmlhttp成功发送了与soap示例所符的soap请求.
‘检测一下是否成功:
response.write xmlhttp.status&” ”
response.write xmlhttp.statustext
set xmlhttp = nothing
%>
如果成功会显示200 ok,不成功会显示 500 内部服务器错误? connection: keep-alive .
成功后就可以利用webservice的响应,如下:
soap响应示例
下面是一个 soap 响应示例。所显示的占位符需要由实际值替换。
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>
<loginbyaccountresponse xmlns="http://tempuri.org/">
<loginbyaccountresult>string</loginbyaccountresult>
</loginbyaccountresponse>
</soap:body>
</soap:envelope>
这是与刚才soap请求示例所对应的soap响应示例,在成功发送请求后,就可以查看该响应 :
if xmlhttp.status = 200 then
set xmldoc =server.createobject("msxml.domdocument")
xmldoc.load(xmlhttp.responsexml)
xmlstr = xmldoc.xml
set xmldoc=nothing
xmlstr = replace(xmlstr,"<","<")
xmlstr = replace(xmlstr,">",">")
response.write xmlstr
else
response.write xmlhttp.status&" "
response.write xmlhttp.statustext
end if
请求正确则给出完整响应,请求不正确(如账号,密码不对)响应的内容就会信息不完整.
取出响应里的数据,如下:
if xmlhttp.status = 200 then
set xmldoc = server.createobject("msxml.domdocument")
xmldoc.load(xmlhttp.responsexml)
response.write xmldoc.documentelement.selectnodes("//loginbyaccountresult")(0).text ‘显示节点为loginbyaccountresult的数据(有编码则要解码)
set xmldoc = nothing
else
response.write xmlhttp.status&" "
response.write xmlhttp.statustext
end if
显示某节点各个属性和数据的function:
function showallnode(rootname,myxmldoc)'望大家不断完鄯 2005-1-9 writed by 844
if rootname<>"" then
set nodeobj=myxmldoc.documentelement.selectsinglenode("//"&rootname&"")'当前结点对像
nodeattributelen=myxmldoc.documentelement.selectsinglenode("//"&rootname&"").attributes.length'当前结点属性数
returnstring=returnstring&"<br>节点名称:"&rootname
if nodeobj.text<>"" then
returnstring=returnstring&"<br>节点的文本:("&nodeobj.text&")"
end if
returnstring=returnstring&"<br>{<br>"
if nodeattributelen<>0 then
returnstring=returnstring&"<br>属性数有 "&nodeattributelen&" 个,分别是:"
end if
for i=0 to nodeattributelen-1
returnstring=returnstring&"<li>"&nodeobj.attributes(i).name&": "&nodeobj.getattribute(nodeobj.attributes(i).name)&" </li>"
next
if nodeobj.childnodes.length<>0 then
if nodeobj.haschildnodes() and lcase(nodeobj.childnodes.item(0).nodename)<>"#text" then'是否有子节点
set childnodeobj=nodeobj.childnodes
childnodelen=nodeobj.childnodes.length
returnstring=returnstring&"<br><br>有 "&childnodelen&" 个子节点;<br>分别是: "
for i=0 to childnodelen-1
returnstring=returnstring&"<li>"&childnodeobj.item(i).nodename&"</li>"
next
end if
end if
returnstring=returnstring&"<br>}<br>"
response.write returnstring
set nodeobj=nothing
end if
end function
可以这样用:
if xmlhttp.status = 200 then
set xmldoc = server.createobject("msxml.domdocument")
xmldoc.load(xmlhttp.responsexml)
showallnode "loginbyaccountresponse",xmldoc’调用showallnode
set xmldoc = nothing
else
response.write xmlhttp.status&" "
response.write xmlhttp.statustext
end if
二.post请求示例
http post
下面是一个 http post 请求示例。所显示的占位符需要由实际值替换。
post /webservice1/usersignon.asmx/loginbyaccount http/1.1
host: 192.100.100.81
content-type: application/x-www-form-urlencoded
content-length: length
username=string&password=string
构造post请求:
<%
url = "http://192.100.100.81/webservice1/usersignon.asmx/loginbyaccount"
soaprequest="username="&username&"&password="&password
set xmlhttp = server.createobject("msxml2.xmlhttp")
xmlhttp.open "post",url,false
xmlhttp.setrequestheader "content-type", "application/x-www-form-urlencoded"’注意
xmlhttp.setrequestheader "host","192.100.100.81"
xmlhttp.setrequestheader "content-length",len(soaprequest)
xmlhttp.send(soaprequest)
‘这样就利用xmlhttp成功发送了与http post示例所符的post请求.
‘检测一下是否成功:
response.write xmlhttp.status&” ”
response.write xmlhttp.statustext
set xmlhttp = nothing
%>
如果成功会显示200 ok,不成功会显示 500 内部服务器错误? connection: keep-alive .
成功后就可以利用webservice的响应,如下:
http post
下面是一个 http post 响应示例。所显示的占位符需要由实际值替换。
http/1.1 200 ok
content-type: text/xml; charset=utf-8
content-length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="stringhttp://tempuri.org/">string</string>
显示:
if xmlhttp.status = 200 then
set xmldoc = server.createobject("msxml.domdocument")
xmldoc.load(xmlhttp.responsexml)
showallnode "string",xmldoc'调用showallnode
set xmldoc = nothing
else
response.write xmlhttp.status&" "
response.write xmlhttp.statustext
end if
用PHP实现Soap通讯
<?
php
function HttpSoap($server, $port, $url, $namespace, $action, $data) {
$fp = @fsockopen($server, $port);
if (!$fp) {
return FALSE;
} else {
$soapData = ConstructData($namespace, $action, $data);
$length = strlen($soapData);
$out = "POST $url HTTP/1.1\r\n";
$out .= "Host: $server\r\n";
$out .= "Content-Type: text/xml; charset=utf-8\r\n";
$out .= "Content-Length: $length\r\n";
$out .= "SOAPAction: \"$namespace$action\"\r\n\r\n";
$out .= $soapData;
$out .= "\r\n\r\n";
fputs($fp, $out);
stream_set_timeout($fp, 2);
$header = "";
while($line = trim(fgets($fp))) {
$header .= $line."\n";
}
$dataPos = strpos($header, "Content-Length: ") + 16;
$dataEnd = strpos($header, "\n", $dataPos);
$dataLength = substr($header, $dataPos, $dataEnd - $dataPos);
$data = "";
if($dataLength > 0) {
$data = fread($fp, $dataLength);
}
fclose($fp);
if(strlen($data) != $dataLength || $dataLength <= 0) {
return FALSE;
}
return $data;
}
}
function ConstructData($namespace, $action, $data) {
$soapData = "
<?xml version="1.0\" encoding=\"utf-8\"?>
\r\n";
$soapData .= "<soap:Envelope xmlns:xsi=\http://www.w3.org/2001/XMLSchema-instance\
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xml
soap.org/soap/envelope/\">\r\n";
$soapData .= " <soap:Body>\r\n";
$soapData .= " <$action xmlns=\"$namespace\">\r\n";
foreach($data as $name => $value) {
$name = iconv("GBK","UTF-8",$name);
$value= iconv("GBK","UTF-8",$value);
$soapData .= " <$name>$value</$name>\r\n";
}
$soapData .= " </$action>\r\n";
$soapData .= " </soap:Body>\r\n";
$soapData .= "</soap:Envelope>";
return $soapData;
}
<?$
data=array('user'=>'测试', //如果需要输入二进制数据请采用BASE64编码
'pass'=>'test');
echo HttpSoap('sample.anyhost.com', 80, '/sampleSoap.asmx', 'http://tempuri.org/', 'logIn', $data);
?>
在前面写了flash9/as3访问WebService的的方法,后来发现这种方法不可行,一旦ws和flash不在同一机器上,flash就不能直接post ws 的方法.
google了一下,发现很多人也在寻找flash9/as3访问WebService的方法(http://www.twelvestone.com/forum_thread/view/35257),搜索了asp和php调用ws的方法, 通常可以使用post/soap的方法访问 WebService,其中使用post的请求方式和我前面介绍的方法一样,可惜使用flash就不行.
那么在没有找到好的方法使用ws之前,能不能用flash9/as3访问WebService呢,我觉得目前有两种替代的方法.
1.用asp,php,asp.net等等来访问ws,然后再用flash去调用这些页面,当然这种方法太不可取了...
2.在同一页面使用两个swf,有一不可见的as2的swf,另外的是as3的要使用的swf,使用as2连接ws,然后as3的swf利用localConnection获取as2访问ws得到的结果...
其实最根本的方法还是直接寻找as3连接ws的方法.
1.改写flash8里面的as2的mx.services包.
2.找到flex的rpc包的源文件
3.寻找另外或者跟简单的方法(flash remoting就可以很方便的利用as3访问,ws也许也有).
//
下面是利用asp和php访问ws的方法,没有利用封装的内容访问,也许能有参考作用(内容摘自论坛,不知道原始地址是哪儿,如果有朋友知道请留言)
//
asp的方法
//
1. soap请求方式
2. post请求方式
---------------------
一.soap请求示例
下面是一个 soap 请求示例。所显示的占位符需要由实际值替换。
post /webservice1/usersignon.asmx http/1.1
host: 192.100.100.81
content-type: text/xml; charset=utf-8
content-length: length
soapaction: "http://tempuri.org/loginbyaccount"
<?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>
<loginbyaccount xmlns="http://tempuri.org/">
<username>string</username>
<password>string</password>
</loginbyaccount>
</soap:body>
</soap:envelope>
为了与webservice交互,需要构造一个与上完全相同的soap请求:
<%
url = "http://192.100.100.81/webservice1/usersignon.asmx"
soaprequest="<?xml version="&chr(34)&"1.0"&chr(34)&" encoding="&chr(34)&"utf-8"&chr(34)&"?>"& _
"<soap:envelope xmlns:xsi="&chr(34)&"http://www.w3.org/2001/xmlschema-instance"&chr(34)&" "& _
"xmlns:xsd="&chr(34)&"http://www.w3.org/2001/xmlschema"&chr(34)&" "& _
"xmlns:soap="&chr(34)&"http://schemas.xmlsoap.org/soap/envelope/"&chr(34)&">"& _
"<soap:body>"& _
"<loginbyaccount xmlns="&chr(34)&"http://tempuri.org/"&chr(34)&">"& _
"<username>"&username&"</username>"& _
"<password>"&password&"</password>"& _
"</loginbyaccount>"& _
"</soap:body>"& _
"</soap:envelope>"
set xmlhttp = server.createobject("msxml2.xmlhttp")
xmlhttp.open "post",url,false
xmlhttp.setrequestheader "content-type", "text/xml;charset=utf-8"
xmlhttp.setrequestheader "host","192.100.100.81"
xmlhttp.setrequestheader "content-length",len(soaprequest)
xmlhttp.setrequestheader "soapaction", "http://tempuri.org/loginbyaccount" ‘一定要与webservice的命名空间相同,否则服务会拒绝
xmlhttp.send(soaprequest)
‘这样就利用xmlhttp成功发送了与soap示例所符的soap请求.
‘检测一下是否成功:
response.write xmlhttp.status&” ”
response.write xmlhttp.statustext
set xmlhttp = nothing
%>
如果成功会显示200 ok,不成功会显示 500 内部服务器错误? connection: keep-alive .
成功后就可以利用webservice的响应,如下:
soap响应示例
下面是一个 soap 响应示例。所显示的占位符需要由实际值替换。
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>
<loginbyaccountresponse xmlns="http://tempuri.org/">
<loginbyaccountresult>string</loginbyaccountresult>
</loginbyaccountresponse>
</soap:body>
</soap:envelope>
这是与刚才soap请求示例所对应的soap响应示例,在成功发送请求后,就可以查看该响应 :
if xmlhttp.status = 200 then
set xmldoc =server.createobject("msxml.domdocument")
xmldoc.load(xmlhttp.responsexml)
xmlstr = xmldoc.xml
set xmldoc=nothing
xmlstr = replace(xmlstr,"<","<")
xmlstr = replace(xmlstr,">",">")
response.write xmlstr
else
response.write xmlhttp.status&" "
response.write xmlhttp.statustext
end if
请求正确则给出完整响应,请求不正确(如账号,密码不对)响应的内容就会信息不完整.
取出响应里的数据,如下:
if xmlhttp.status = 200 then
set xmldoc = server.createobject("msxml.domdocument")
xmldoc.load(xmlhttp.responsexml)
response.write xmldoc.documentelement.selectnodes("//loginbyaccountresult")(0).text ‘显示节点为loginbyaccountresult的数据(有编码则要解码)
set xmldoc = nothing
else
response.write xmlhttp.status&" "
response.write xmlhttp.statustext
end if
显示某节点各个属性和数据的function:
function showallnode(rootname,myxmldoc)'望大家不断完鄯 2005-1-9 writed by 844
if rootname<>"" then
set nodeobj=myxmldoc.documentelement.selectsinglenode("//"&rootname&"")'当前结点对像
nodeattributelen=myxmldoc.documentelement.selectsinglenode("//"&rootname&"").attributes.length'当前结点属性数
returnstring=returnstring&"<br>节点名称:"&rootname
if nodeobj.text<>"" then
returnstring=returnstring&"<br>节点的文本:("&nodeobj.text&")"
end if
returnstring=returnstring&"<br>{<br>"
if nodeattributelen<>0 then
returnstring=returnstring&"<br>属性数有 "&nodeattributelen&" 个,分别是:"
end if
for i=0 to nodeattributelen-1
returnstring=returnstring&"<li>"&nodeobj.attributes(i).name&": "&nodeobj.getattribute(nodeobj.attributes(i).name)&" </li>"
next
if nodeobj.childnodes.length<>0 then
if nodeobj.haschildnodes() and lcase(nodeobj.childnodes.item(0).nodename)<>"#text" then'是否有子节点
set childnodeobj=nodeobj.childnodes
childnodelen=nodeobj.childnodes.length
returnstring=returnstring&"<br><br>有 "&childnodelen&" 个子节点;<br>分别是: "
for i=0 to childnodelen-1
returnstring=returnstring&"<li>"&childnodeobj.item(i).nodename&"</li>"
next
end if
end if
returnstring=returnstring&"<br>}<br>"
response.write returnstring
set nodeobj=nothing
end if
end function
可以这样用:
if xmlhttp.status = 200 then
set xmldoc = server.createobject("msxml.domdocument")
xmldoc.load(xmlhttp.responsexml)
showallnode "loginbyaccountresponse",xmldoc’调用showallnode
set xmldoc = nothing
else
response.write xmlhttp.status&" "
response.write xmlhttp.statustext
end if
二.post请求示例
http post
下面是一个 http post 请求示例。所显示的占位符需要由实际值替换。
post /webservice1/usersignon.asmx/loginbyaccount http/1.1
host: 192.100.100.81
content-type: application/x-www-form-urlencoded
content-length: length
username=string&password=string
构造post请求:
<%
url = "http://192.100.100.81/webservice1/usersignon.asmx/loginbyaccount"
soaprequest="username="&username&"&password="&password
set xmlhttp = server.createobject("msxml2.xmlhttp")
xmlhttp.open "post",url,false
xmlhttp.setrequestheader "content-type", "application/x-www-form-urlencoded"’注意
xmlhttp.setrequestheader "host","192.100.100.81"
xmlhttp.setrequestheader "content-length",len(soaprequest)
xmlhttp.send(soaprequest)
‘这样就利用xmlhttp成功发送了与http post示例所符的post请求.
‘检测一下是否成功:
response.write xmlhttp.status&” ”
response.write xmlhttp.statustext
set xmlhttp = nothing
%>
如果成功会显示200 ok,不成功会显示 500 内部服务器错误? connection: keep-alive .
成功后就可以利用webservice的响应,如下:
http post
下面是一个 http post 响应示例。所显示的占位符需要由实际值替换。
http/1.1 200 ok
content-type: text/xml; charset=utf-8
content-length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="stringhttp://tempuri.org/">string</string>
显示:
if xmlhttp.status = 200 then
set xmldoc = server.createobject("msxml.domdocument")
xmldoc.load(xmlhttp.responsexml)
showallnode "string",xmldoc'调用showallnode
set xmldoc = nothing
else
response.write xmlhttp.status&" "
response.write xmlhttp.statustext
end if
用PHP实现Soap通讯
<?
php
function HttpSoap($server, $port, $url, $namespace, $action, $data) {
$fp = @fsockopen($server, $port);
if (!$fp) {
return FALSE;
} else {
$soapData = ConstructData($namespace, $action, $data);
$length = strlen($soapData);
$out = "POST $url HTTP/1.1\r\n";
$out .= "Host: $server\r\n";
$out .= "Content-Type: text/xml; charset=utf-8\r\n";
$out .= "Content-Length: $length\r\n";
$out .= "SOAPAction: \"$namespace$action\"\r\n\r\n";
$out .= $soapData;
$out .= "\r\n\r\n";
fputs($fp, $out);
stream_set_timeout($fp, 2);
$header = "";
while($line = trim(fgets($fp))) {
$header .= $line."\n";
}
$dataPos = strpos($header, "Content-Length: ") + 16;
$dataEnd = strpos($header, "\n", $dataPos);
$dataLength = substr($header, $dataPos, $dataEnd - $dataPos);
$data = "";
if($dataLength > 0) {
$data = fread($fp, $dataLength);
}
fclose($fp);
if(strlen($data) != $dataLength || $dataLength <= 0) {
return FALSE;
}
return $data;
}
}
function ConstructData($namespace, $action, $data) {
$soapData = "
<?xml version="1.0\" encoding=\"utf-8\"?>
\r\n";
$soapData .= "<soap:Envelope xmlns:xsi=\http://www.w3.org/2001/XMLSchema-instance\
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xml
soap.org/soap/envelope/\">\r\n";
$soapData .= " <soap:Body>\r\n";
$soapData .= " <$action xmlns=\"$namespace\">\r\n";
foreach($data as $name => $value) {
$name = iconv("GBK","UTF-8",$name);
$value= iconv("GBK","UTF-8",$value);
$soapData .= " <$name>$value</$name>\r\n";
}
$soapData .= " </$action>\r\n";
$soapData .= " </soap:Body>\r\n";
$soapData .= "</soap:Envelope>";
return $soapData;
}
<?$
data=array('user'=>'测试', //如果需要输入二进制数据请采用BASE64编码
'pass'=>'test');
echo HttpSoap('sample.anyhost.com', 80, '/sampleSoap.asmx', 'http://tempuri.org/', 'logIn', $data);
?>
[本日志由 roading 于 2007-07-17 11:46 PM 编辑]
Tags: flash9 as3 webservice
相关日志:
* as3的hit检测[2843]
* as3右键响应事件--使用js屏蔽flash的右键菜单[3391]
* as3:传递url参数给加载进来的swf[2465]
* flash develop3[2686]
* 使用flash9(as3)连接webservice[5722]
* flash cs3(as3)访问WebService类包下载[2555]
Tags: flash9 as3 webservice
相关日志:* as3的hit检测[2843]
* as3右键响应事件--使用js屏蔽flash的右键菜单[3391]
* as3:传递url参数给加载进来的swf[2465]
* flash develop3[2686]
* 使用flash9(as3)连接webservice[5722]
* flash cs3(as3)访问WebService类包下载[2555]
评论: 0 | 查看次数: 3991
发表评论
订阅
上一篇
下一篇





