java 调用webservice 接口发短信

wlaqianqian34 2012-01-17 11:47:27
就给了个地址:http://61.191.40.242:9090/WebService/EntInterface.asmx

要查看接口的定义的话则在连接地址下面增加“?WSDL”,

即http://61.191.40.242:9090/WebService/EntInterface.asmx?WSDL

请问根据这个如何实现java发短信?
...全文
298 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ai523730098 2012-01-17
  • 打赏
  • 举报
回复
weather.xml的内容是

<?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>
<getWeatherbyCityName xmlns="http://WebXml.com.cn/">
<theCityName>${city}$</theCityName>
</getWeatherbyCityName>
</soap:Body>
</soap:Envelope>

放工程目录下就行了
ai523730098 2012-01-17
  • 打赏
  • 举报
回复
我可以给你两个例子参考下
第一个根据城市取天气
webservice的地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
运行下面的代码试试,实际上就是发请求给这个地址,请求的内容是有一定规则的xml字符串而已。
调用websevice接口的时候,可以首先用soapui调试下。
也可以用工具生成java代码的,生成代码的方法有很多,网上试试

package myweather;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

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 MyWeather {
private static String getSoapRequest(String city) {
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream("weather.xml"));
BufferedReader reader = new BufferedReader(isr);
String soap = "";
String tmp;
while ((tmp = reader.readLine()) != null) {
soap += tmp;
}
reader.close();
isr.close();
return soap.replace("${city}$", city);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

private static InputStream getSoapInputStream(String city) throws Exception {
try {
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
URLConnection 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("SOAPAction","http://WebXml.com.cn/getWeatherbyCityName");

OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
// osw.write(new String(soap.getBytes(),"utf-8"));
osw.write(soap);
osw.flush();
osw.close();

InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static String getWeather(String city) {
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(city);
doc = db.parse(is);
NodeList nl = doc
.getElementsByTagName("getWeatherbyCityNameResult");
Node n = nl.item(0);
// JOptionPane.showMessageDialog(null,n.getChildNodes().getLength());
for (int i = 0; i < n.getChildNodes().getLength() - 1; i++) {
String weather = n.getChildNodes().item(i).getTextContent();
System.out.print(weather);
}
is.close();
return "null";
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static void main(String[] args) throws Exception {
// System.out.println(MyWeather.getWeather("上海"));
MyWeather.getWeather("长沙");
}

}

// 将以下XML保存为weather.xml

/*
* XML:<?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>
* <getWeatherbyCityName xmlns="http://WebXml.com.cn/">
* <theCityName>${city}$</theCityName> </getWeatherbyCityName> </soap:Body>
* </soap:Envelope>
*/

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧