Java实现WebServices不需要Web 应用服务器
Java实现WebServices不需要Web 应用服务器
?
?
????? 近日来,在社区内浏览了一部分关于Java来实现WebServives的帖子,发现其中90%以上都有这样一步操作
?
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class HttpPostTest { void testPost(String urlStr) { try { URL url = new URL(urlStr); URLConnection con = url.openConnection(); con.setDoOutput(true); con.setRequestProperty("Pragma:", "no-cache"); con.setRequestProperty("Cache-Control", "no-cache"); con.setRequestProperty("Content-Type", "text/xml"); OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream()); String xmlInfo = getXmlInfo(); out.write(new String(xmlInfo)); out.flush(); out.close(); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String line = ""; StringBuffer buf = new StringBuffer(); for (line = br.readLine(); line != null; line = br.readLine()) { buf.append(new String(line.getBytes(),"UTF-8")); } System.out.println(buf.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private String getXmlInfo() { // 通过wsdl文件可以查看接口xml格式数据,构造调用接口xml数据 String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">" + "<SOAP-ENV:Body>" + "<m:getItemDetailSingle xmlns:m="http:xxxxxxxxxxxxxxxxxx/">" + "<itemMo>" + "<category>政务域名</category>" + "<city>北京西坝河北里</city>" + "<flag>3</flag>" + "<itemId>10</itemId>" + "<itemIndex>22</itemIndex>" + "<keyword>朝阳区</keyword>" + "<mobile>139-0111-1111</mobile>" + "<password>iteyePl</password>" + "<userName>hwak</userName>" + "</itemMo>" + "</m:getItemDetailSingle>" + "</SOAP-ENV:Body>" + "</SOAP-ENV:Envelope>"; return xml; } public static void main(String[] args) throws UnsupportedEncodingException { String url = "http://localhost:9999/dataService/services/Job"; new HttpPostTest().testPost(url); } }</PRE>?
?
第二个例子:socke方式实现例子:
?
import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class WebServiceClient { /** * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { Socket socket = new Socket("localhost",9003); OutputStream os = socket.getOutputStream(); InputStream is = socket.getInputStream(); //System.out.println(socket.isConnected()); String httpSend = "POST /dataService/services/Job HTTP/1.1\r\n" + "Content-Type:text/xml\r\n" + "Host:localhost:9003\r\n" + "Content-Length:1024\r\n" + "\r\n" + "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">" + "<SOAP-ENV:Body>" + "<m:getItemDetailSingle xmlns:m="http://localhost/">" + "<itemMo>" + "<category>工厂类</category>" + "<city>北京</city>" + "<flag>1</flag>" + "<itemId>0</itemId>" + "<itemIndex>1</itemIndex>" + "<keyword>String</keyword>" + "<mobile>2147483647</mobile>" + "<password>123456</password>" + "<userName>sohu</userName>" + "</itemMo>" + "</m:getItemDetailSingle>" + "</SOAP-ENV:Body>" + "</SOAP-ENV:Envelope>"; os.write(httpSend.getBytes()); os.flush(); InputStreamReader ireader = new InputStreamReader(is); java.io.BufferedReader breader = new java.io.BufferedReader(ireader); String responseLine = ""; while((responseLine = breader.readLine()) != null) { System.out.println(new String(responseLine.getBytes(),"UTF-8")); } System.out.println(""); }}?