通過URL的方式請求WebService服務
阿新 • • 發佈:2019-02-07
1.建立服務端
package com.test;import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class WebTest {
@WebMethod
public void testWebservice(String data){
System.out.print("接受到請求:" + data);
}
public static void main(String[] args) {
System.out.println("開始釋出");
WebTest test = new WebTest();
Endpoint.publish("
System.out.println("釋出成功");
}
}
如果釋出成功可以看見下面結果: <?xml version="1.0" encoding="UTF-8" ?>
<!--Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!--Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://test.com/" name="WebTestService">
<types>
<xsd:schema>
<xsd:import namespace="http://test.com/" schemaLocation="http://localhost:8000/test?xsd=1" />
</xsd:schema>
</types>
<message name="testWebservice">
<part name="parameters" element="tns:testWebservice" />
</message>
<message name="testWebserviceResponse">
<part name="parameters" element="tns:testWebserviceResponse" />
</message>
<portType name="WebTest">
<operation name="testWebservice">
<input message="tns:testWebservice" />
<output message="tns:testWebserviceResponse" />
</operation>
</portType>
<binding name="WebTestPortBinding" type="tns:WebTest">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name="testWebservice">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="WebTestService">
<port name="WebTestPort" binding="tns:WebTestPortBinding">
<soap:address location="http://localhost:8000/test" />
</port>
</service>
</definitions>
2.客戶端通過URLConnection呼叫
package com.ceb.czsczj.client.init;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.ceb.czsczj.client.bo.AccountInfoBO;
import com.ceb.czsczj.client.common.util.DateTools;
/**一個簡單的請求的例子還沒有做完*/
public class ClientTest {
/**
* @Description: demo
* @throws IOException
* @throws DocumentException
* @author fengjiahuan
* @date 2018-2-9上午10:49:25
*===========================================
* 修改人:, 修改時間:, 修改版本:
* 修改備註:
*===========================================
*/
public static void main(String[] args) throws IOException, DocumentException {
ArrayList<AccountInfoBO> list = new ArrayList<AccountInfoBO>();
Map<Object, Object> resMap = new HashMap<Object, Object>();
resMap.put("result", "error");
//建立url地址
URL url = new URL("http://localhost:8000/test");
//開啟連線
URLConnection conn = url.openConnection();
//轉換成HttpURL
HttpURLConnection httpConn = (HttpURLConnection) conn;
//開啟輸入輸出的開關
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
//設定請求方式
httpConn.setRequestMethod("POST");
//設定請求的頭資訊
httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
String date = DateTools.getSysDateStr();
date = date.replaceAll("-", "");
String data = "<burlap:call>" +
"<method>createBusinessData</method>" +
"<string>BANK.****</string>" +
"<string>ABANKTEST</string>" +
"<int>"+date.substring(0, 4)+"</int>" +
"<list>"+ "</list>" + "</burlap:call>";
//獲得輸出流
OutputStream out = httpConn.getOutputStream();
//傳送資料
out.write(data.getBytes());
out.close();
System.out.println(data);
//判斷請求成功
if(httpConn.getResponseCode() == 200){
System.out.println("sss");
//獲得輸入流
InputStream in = httpConn.getInputStream();
//使用輸入流的緩衝區
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
in.close();
StringBuffer sb = new StringBuffer();
String line = null;
//讀取輸入流
while((line = reader.readLine()) != null){
sb.append(line);
}
//建立sax的讀取器
SAXReader saxReader = new SAXReader();
//建立文件物件
Document doc = saxReader.read(new StringReader(sb.toString()));
//獲得請求響應return元素
List<Element> eles = doc.selectNodes("//list");
for(Element ele : eles){
System.out.println(ele.getText());
}
}
httpConn.disconnect();
/*返回報文資料項*/
resMap.put("result", "success");
System.out.println("請求成功!!");
}
}