一、使用WebClient方式实现调用;
1.WebService接口示例
POST /WebService.asmx HTTP/1.1
Host: tmscn-dev.carlsberg.asia
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetQrCode"
<?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:Header>
<WSAuthentication xmlns="http://tempuri.org/">
<UserName>string</UserName>
<Password>string</Password>
</WSAuthentication>
</soap:Header>
<soap:Body>
<GetQrCode xmlns="http://tempuri.org/">
<BeginDate>string</BeginDate>
<EndDate>string</EndDate>
</GetQrCode>
</soap:Body>
</soap:Envelope>
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>
<GetQrCodeResponse xmlns="http://tempuri.org/">
<GetQrCodeResult>string</GetQrCodeResult>
</GetQrCodeResponse>
</soap:Body>
</soap:Envelope>
2.代码如下:
package com.sigmatrix.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@Component
public class WebServiceUtils {
private static Logger logger = LoggerFactory.getLogger(WebServiceUtils.class);
private static String qrCodeUrl = https://tmscn-dev.carlsberg.asia:8443/WebService.asmx
/** * 请求头 * @return */
public static String getSoapHeader(String userName, String passWord){
StringBuffer soapHeader = new StringBuffer();
soapHeader.append("<soap:Header>");
soapHeader.append("<WSAuthentication xmlns=\"http://tempuri.org/\">");
soapHeader.append("<UserName>"+userName+"</UserName>");
soapHeader.append("<Password>"+passWord+"</Password>");
soapHeader.append("</WSAuthentication>");
soapHeader.append("</soap:Header>");
return soapHeader.toString();
}
/** * 请求体 * @return */
public static String getAccInfoXml(String userName, String passWord, String beginDate, String endDate){
StringBuffer template = new StringBuffer();
String header = getSoapHeader(userName, passWord);
template.append("<?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/\">");
template.append(header);
template.append("<soap:Body>");
template.append("<GetQrCode xmlns=\"http://tempuri.org/\">");
template.append("<BeginDate>"+beginDate+"</BeginDate>");
template.append("<EndDate>"+endDate+"</EndDate>");
template.append("</GetQrCode>");
template.append("</soap:Body>");
template.append("</soap:Envelope>");
return template.toString();
}
/** * 访问WebService接口,获取数据 * @return */
public static String getAccInfo(String userName, String passWord, String beginDate, String endDate){
String urlStr = qrCodeUrl;
String paraXml = getAccInfoXml(userName, passWord, beginDate, endDate);
String soapAction ="http://tempuri.org/GetQrCode";
OutputStream out = null;
String resultData = "";
try {
URL url = new URL(urlStr);
HttpURLConnection con;
con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
con.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
con.setRequestProperty("SOAPAction", soapAction);
out = con.getOutputStream();
con.getOutputStream().write(paraXml.getBytes());
out.flush();
out.close();
int code = con.getResponseCode();
String tempString = null;
StringBuffer sb1 = new StringBuffer();
if (code == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
while ((tempString = reader.readLine()) != null) {
sb1.append(tempString);
}
if (null != reader) {
reader.close();
}
} else {
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getErrorStream(), "UTF-8"));
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
sb1.append(tempString);
}
if (null != reader) {
reader.close();
}
}
//响应报文
resultData = sb1.toString();
System.out.println(resultData);
} catch (Exception e) {
logger.error("访问webService接口异常", e);
}
return resultData;
}
}
二、根据wsdl文件来生成java-ws调用WebService接口
1.在cmd中执行以下命令,在指定目录中自动生成代码,调用webservice接口。
wsimport -XadditionalHeaders -s d:\webservice2 -p com.example.demo.request -encoding utf-8 https://tmscn-dev.carlsberg.asia:8443/WebService.asmx?WSDL
三、根据WebService完整的访问路径和用户密码生成代码
1.创建java项目;
2.添加框架依赖
3.选择webservices client
4.使用idea生成相关代码
5.客户端测试
自动生成的是lib包,相对应的pom依赖
<!-- webservice start -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.4</version>
</dependency>
<!-- webservice end -->
今天的文章Java对接WebService接口分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/67274.html