Soap: Simple Object Access Protocol ——简单对象访问协议
1.soap作为一个基于xml语言的协议用于网上传输数据
2.SOAP = HTTP + XML
3.SOAP是基于HTTP的 (这里顺便补充一下,Http是基于Socket的)
4.SOAP的组成如下
1.Envelope: 必须的,以XML根元素出现
2.Header:可选的
3.Body:必须的,在body部分包含一些,执行服务器的方法,和发送到服务器的数据
/**
* soap较rest具有比较好的可靠性与安全性
*
* soap是一种固定的规范
*
* 能够满足应用上下文信息与对话状态管理
*
* @author xxx
*
*/
public class HttpUrlConnectionSoap {
private static final Logger LOG = LoggerFactory.getLogger(HttpUrlConnectionSoap.class);
public static String Get(List<String> params) {
String url = "http://localhost:8080/Base/testGet?username=" + params.get(0);
int timeout = 5000;
try {
// httpUrlConnection发送Soap请求
URL url2 = new URL(url);
// 创建httpUrlConnection对象
HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
// 设置超时时间
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
// 设置请求方式
connection.setRequestMethod("GET");
// 是否使用缓存
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// 执行请求返回响应码
int responseCode = connection.getResponseCode();
String responseMsg = "";
// 通过响应码,判断连接是否成功
if (responseCode >= HttpsURLConnection.HTTP_OK && responseCode <= HttpsURLConnection.HTTP_BAD_REQUEST) {
// 连接成功,获取响应内容
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = "";
if ((line = reader.readLine()) != null) {
responseMsg += line + "\n";
}
}
return responseMsg;
} catch (IOException e) {
LOG.error("IOException", e);
}
return null;
}
public static String Post(List<String> params) {
String url = "http://localhost:8080/Base/testPost";
int timeout = 5000;
// 线程安全
StringBuffer buffer = new StringBuffer("");
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buffer.append("<soap:Envelope " + "xmlns:api='http://demo.ls.com/' "
+ "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/'>");
buffer.append("<soap:Body>");
buffer.append("<api:sayHello>");
buffer.append("<arg0>ls</arg0>");
buffer.append("</api:sayHello>");
buffer.append("</soap:Body>");
buffer.append("</soap:Envelope>");
try {
// httpUrlConnection发送Soap请求
URL url2 = new URL(url);
// 创建httpUrlConnection对象
HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
// 设置超时时间
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
// 设置请求方式
connection.setRequestMethod("POST");
// 是否使用缓存
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
writer.write(buffer.toString());
writer.flush();
// 执行请求返回响应码
int responseCode = connection.getResponseCode();
String responseMsg = "";
// 通过响应码,判断连接是否成功
if (responseCode >= HttpsURLConnection.HTTP_OK && responseCode <= HttpsURLConnection.HTTP_BAD_REQUEST) {
// 连接成功,获取响应内容
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = "";
if ((line = reader.readLine()) != null) {
responseMsg += line + "\n";
}
}
return responseMsg;
} catch (IOException e) {
LOG.error("IOException", e);
}
return null;
}
}