package com.rl.client; import java.io.BufferedInputStream; import java.io.BufferedReader; 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.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class TestHttpURLConnection { public static void main(String[] args) throws URLException { URL url = new URL("http://127.0.0.1:8080/hello?wsdl"); URLConnection uc = url.openConnection(); HttpURLConnection httpuc = (HttpURLConnection) uc; httpuc.setDoInput(true); httpuc.setDoOutput(true);//开启输入输出 httpuc.setRequestMethod("POST"); httpuc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); String data = "<soapenv:Envelop xmlns:soapenv="http://schemas.xmlsoap.org/soap">" + "<soapenv:Body>" + "<q0:sayBye>" + "<arg0>wangwu</arg0>" + "</q0:sayBye>" + "</soapenv:Body>" + "</soapenv:Envelope>"; OutputStream out = httpuc.getOutputStream(); out.write(data.getBytes());//已经把消息给写过去了. if(httpuc.getResponseCode()==200){ InputStream in = httpuc.getInputStream();//通过输入流把消息接收完 BufferedReader br = new BufferedReader(new InputStreamReader(in)); //写Socket的那种输入流的方式来读它 String line = null; StringBuffer sb = new StringBuffer(); while((line = br.readLine())!=null ){ sb.append(line); } //StringBuffer是一个可变字符串,我们要把它变成一个文档来解析 //java代码里面解析XML SAXReader reader = new SAXReader(); Document doc = reader.read(new StringReader(sb.toString()));//它的返回值是一个文档对象Document List<Element> eList = doc.selectNodes("//return"); for (Element element : eList) { System.out.println(element.getText()); } } } }