java调用WebService(客户端)
看下了网上大部分都是写java来编写WS服务端,小编这边就小写了下JAVA的调用端。
WebService可以有Get、 Post、Soap、Document四种方式调用,以下是四种方式的参照说明。
name | 属性 | 说明 |
HttpGet | 添加HTTP GET协议 | 在追加到HTTP请求URL的查询字符串中传递的方法参数,格式为:?name1=value1&。返回值被当做简单的XML文档放入HTTP响应的正文中(没有<soap:Envelope>)。 |
HTTPPost | 添加HTTP POST协议 | 在HTTP请求的正文中传递的方法参数,格式为:name1=value1&。返回值被当做简单的XML文档放入HTTP响应的正文中(没有< soap:Envelope>)。 |
HTTPSoap | 添加Http Soap协议 | Soap消息在HTTP请求的正文中发送;Soap响应在HTTP响应的正文中发送。 |
Documentation | 添加特殊的Documentation协议 | 当在启用了此协议的情况下直接请求.asmx页时,Asp.Net运行Helper页创建HTML文档页,该文档页被传递到提出请求的客户端 |
对于SOAP协议多写了个CXF的调用(对于CXF必须有以下包:)
以下是调用代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.URL;
import java.URLConnection;
import java.URLEncoder;
import f.endpoint.Client;
import dpoint.dynamic.JaxWsDynamicClientFactory;
/**
* 功能描述:WebService调用
*
*/
public class ClientTest {
/**
* 功能描述:HTTP-GET
*
*/
public String get() {
URL url;
BufferedReader bin = null;
StringBuilder result = new StringBuilder();
try {
String urlTemp = "www.webxml//WebServices/WeatherWebService.asmx/getSupportCity?byProvinceName="
+ de("福建", "UTF-8");// URLEncoder用来参数编码
url = new URL(urlTemp);
InputStream in = url.openStream(); // 请求
bin = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String s = null;
while ((s = adLine()) != null) {
result.append(s);
}
} catch (Exception e) {
java stream e.printStackTrace();
} finally {
if (null != bin) {
try {
bin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String();
}
/**
* 功能描述:HTTP-POST
*
*/
public String post() {
OutputStreamWriter out = null;
StringBuilder sTotalString = new StringBuilder();
try {
URL urlTemp = new URL(
"www.webxml/WebServices/WeatherWebService.asmx/getSupportCity");
URLConnection connection = urlTemp.openConnection();
connection.setDoOutput(true);
out = new OutputStream(), "UTF-8");
StringBuffer sb = new StringBuffer();
sb.append("byProvinceName=福建");
out.String());
out.flush();
String sCurrentLine;
sCurrentLine = "";
InputStream l_urlStream;
l_urlStream = InputStream();// 请求
BufferedReader l_reader = new BufferedReader(new InputStreamReader(
l_urlStream));
while ((sCurrentLine = adLine()) != null) {
sTotalString.append(sCurrentLine);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String();
}
/**
* 功能描述: 请求 HTTP-SOAP
*
*/
public String getSoapInputStream() {
try {
String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"/2001/XMLSchema-instance\" xmlns:xsd=\"/2001/XMLSchema\" xmlns:soap=\"/soap/envelope/\"><soap:Body><getSupportCity xmlns=\"WebXml/\"><byProvinceName></byProvinceName></getSupportCity></soap:Body></soap:Envelope>";
URL url = new URL(
"www.webxml/WebServices/WeatherWebService.asmx?wsdl");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", String(soap
.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction",
"WebXml/getSupportCity");
OutputStream os = OutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
StringBuilder sTotalString = new StringBuilder();
String sCurrentLine = "";
InputStream is = InputStream();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(
is));
while ((sCurrentLine = adLine()) != null) {
sTotalString.append(sCurrentLine);
}
String();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 功能描述:使用CXF 请求 HTTP-SOAP
*
*/
public String soap() {
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory
.newInstance();
String url = "www.webxml/webservices/qqOnlineWebService.asmx?wsdl";// www.fjyxd:17001/DefDispatcher/dispatche?wsdl
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论