get.java
来自「基于eclipse环境下开发的cxf例程」· Java 代码 · 共 84 行
JAVA
84 行
package demo.hw.client;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.Properties;import javax.xml.transform.OutputKeys;import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;public final class Get { private Get() { } public static void main(String args[]) throws Exception { String target = null; URL url = null; HttpURLConnection httpConnection = null; InputStream in = null; StreamSource source = null; // Sent HTTP GET request to invoke greetMe FAULT target = "http://localhost:9000/SoapContext/SoapPort/greetMe/me/CXF"; url = new URL(target); httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.connect(); System.out.println("Invoking server through HTTP GET to invoke greetMe"); try { in = httpConnection.getInputStream(); source = new StreamSource(in); printSource(source); } catch (Exception e) { System.err.println("GreetMe Fault: " + e.getMessage()); } InputStream err = httpConnection.getErrorStream(); source = new StreamSource(err); printSource(source); // Sent HTTP GET request to invoke greetMe target = "http://localhost:9000/SoapContext/SoapPort/greetMe/requestType/"+args[1]; url = new URL(target); httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.connect(); System.out.println("Invoking server through HTTP GET to invoke greetMe"); in = httpConnection.getInputStream(); source = new StreamSource(in); printSource(source); } private static void printSource(Source source) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); StreamResult sr = new StreamResult(bos); Transformer trans = TransformerFactory.newInstance().newTransformer(); Properties oprops = new Properties(); oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperties(oprops); trans.transform(source, sr); System.out.println(); System.out.println("**** Response ******"); System.out.println(); System.out.println(bos.toString()); bos.close(); System.out.println(); } catch (Exception e) { e.printStackTrace(); } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?