各种开发语言示例调用WebService接口
ASP示例:
<%
uid="账号"
pwd="密码"
tos="13900041123"
msg="你们好"
url = "http://URL/Service.asmx/SendMessages"
SoapRequest="uid="&uid&"&pwd="&pwd&"&tos="&tos&"&msg="&msg&"&otime="
''''''''''''''''''''''''''以下代码不变''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"'注意
xmlhttp.setRequestHeader "HOST","URL"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.Send(SoapRequest)
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
Set xmlhttp = Nothing
Function showallnode(rootname,myxmlDOC)
set nodeobj=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"")'当前结点对像
if nodeobj.text<>"" then
returnstring=returnstring&"返回值:"&nodeobj.text
end if
response.write returnstring
set nodeobj=nothing
End Function
%>
或者
function SendMessages(uid,pwd,tos,msg,otime)
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>"& _
"<SendMessages xmlns="&CHR(34)&"http://tempuri.org/"&CHR(34)&">"& _
"<uid>"&uid&"</uid>"& _
"<pwd>"&pwd&"</pwd>"& _
"<tos>"&tos&"</tos>"& _
"<msg>"&msg&"</msg>"& _
"<otime>"&otime&"</otime>"& _
"</SendMessages>"& _
"</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","URL"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/SendMessages" '一定要与WEBSERVICE的命名空间相同,否则服务会拒绝
xmlhttp.Send(SoapRequest)
''样就利用XMLHTTP成功发送了与SOAP示例所符的SOAP请求.'检测一下是否返回200=成功:
If xmlhttp.Status = 200 Then
Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
xmlDOC.load(xmlhttp.responseXML)
SendMessages=xmlDOC.documentElement.selectNodes("//SendMessagesResult")(0).text '显示节点为GetUserInfoResult的数据(返回字符串)
Set xmlDOC = nothing
Else
SendMessages=xmlhttp.Status&" "
SendMessages=xmlhttp.StatusText
End if
Set xmlhttp = Nothing
end function
Delphi示例:
procedure TForm1.Button2Click(Sender: TObject);
var
uid,pwd,mob,txt:WideString;
Iservice: Service1Soap;
back_info:string;
begin
HTTPRIO1.URL:=service_url.Text;
HTTPRIO1.HTTPWebNode.Agent := 'Borland SOAP 1.2';
HTTPRIO1.HTTPWebNode.UseUTF8InHeader := true;
Iservice:= HTTPRIO1 as Service1Soap;
//______________
uid:=euid.Text;
pwd:=epwd.Text;
mob:=emobno.Text;
txt:=econtent.Text;
back_info:=Iservice.SendMessages(uid,pwd,mob,txt,'');
memo2.Text:=back_info;
if length(trim(back_info))>3 then begin
showmessage('短信发送成功'+back_info);
end else begin
showmessage('短信发送失败'+back_info);
end;
end;
注:
initialization
InvRegistry.RegisterInterface(TypeInfo(Service1Soap), 'http://tempuri.org/', 'utf-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(Service1Soap), 'http://tempuri.org/%operationName%');
//delphi调用net2.0需要加这一行。否则会出错。
InvRegistry.RegisterInvokeOptions(TypeInfo(Service1Soap), ioDocument);
end.
JAVA示例:
需要导入axis.jar
package server;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLConnection;
import java.net.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class smsService {
private String getSoapSmssend(String userid,String pass,String mobiles,String msg,String time)
{
try
{
String soap = "";
soap = "<?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>"
+"<SendMessages xmlns=\"http://tempuri.org/\">"
+"<uid>"+userid+"</uid>"
+"<pwd>"+pass+"</pwd>"
+"<tos>"+mobiles+"</tos>"
+"<msg>"+msg+"</msg>"
+"<otime>"+time+"</otime>"
+"</SendMessages>"
+"</soap:Body>"
+"</soap:Envelope>";
return soap;
}
catch (Exception ex)
{
ex.printStackTrace();
return null;
}
}
private InputStream getSoapInputStream(String userid,String pass,String mobiles,String msg,String time)throws Exception
{
URLConnection conn = null;
InputStream is = null;
try
{
String soap=getSoapSmssend(userid,pass,mobiles,msg,time);
if(soap==null)
{
return null;
}
try{
URL url=new URL("http://URL/Service.asmx");
conn=url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("HOST","URL");
conn.setRequestProperty("SOAPAction","\"http://tempuri.org/SendMessages\"");
OutputStream os=conn.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8");
osw.write(soap);
osw.flush();
}catch(Exception ex){
System.out.print("SmsSoap.openUrl error:"+ex.getMessage());
}
try{
is=conn.getInputStream();
}catch(Exception ex1){
System.out.print("SmsSoap.getUrl error:"+ex1.getMessage());
}
return is;
}
catch(Exception e)
{
System.out.print("SmsSoap.InputStream error:"+e.getMessage());
return null;
}
}
//发送短信
public String sendSms(String userid,String pass,String mobiles,String msg,String time)
{
String result = "-12";
try
{
Document doc;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db=dbf.newDocumentBuilder();
InputStream is=getSoapInputStream(userid,pass,mobiles,msg,time);
if(is!=null){
doc=db.parse(is);
NodeList nl=doc.getElementsByTagName("SendMessagesResult");
Node n=nl.item(0);
result=n.getFirstChild().getNodeValue();
is.close();
}
return result;
}
catch(Exception e)
{
System.out.print("SmsSoap.sendSms error:"+e.getMessage());
return "-12";
}
}
}
PHP示例:
<?php
$uid = "账号";//用户账户
$pwd = "密码";//用户密码
$mobno = "手机号码";//发送的手机号码,多个请以英文逗号隔开如"138000138000,138111139111"
$content = "发送内容";//发送内容
$otime = '';//定时发送,暂不开通,为空
$client = new SoapClient("URL/Service.asmx?WSDL");
$param = array('uid' => $uid,'pwd' => $pwd,'tos' => $mobno,'msg' => $content,'otime'=>$otime);
$result = $client->__soapCall('SendMessages',array('parameters' => $param));
var_dump($result);
die();
?>
VB.NET示例:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim objSoap As Object, url As String
url = "URL/Service.asmx?wsdl"
objSoap = CreateObject("MSSOAP.SOAPClient30")
objSoap.ClientProperty("ServerHTTPRequest") = True
objSoap.MSSoapInit(url)
txtReturn.Text = objSoap.SendMessages(txtName.Text, txtPwd.Text, txtPhone.Text, txtContent.Text, "")
End Sub
VB示例:
Private Sub Command1_Click()
Dim mySoap As New MSSOAPLib30.SoapClient30
mySoap.ClientProperty("ServerHTTPRequest") = True
mySoap.MSSoapInit "URL/Service.asmx?WSDL"
txtReturn.Text = mySoap.SendMessages(txtName.Text, txtPwd.Text, txtPhone.Text, txtContent.Text, "")
Set mySoap = Nothing
End Sub
VC示例:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
TService:: Service1 ^v = gcnew TService:: Service1;//添加Web引用
txtReturn ->Text = v -> SendMessages(txtName ->Text,txtPwd->Text,txtPhone->Text,txtContent->Text,"");
}
};