使用SOAP UI查看请求参数,将请求参数以xml形式进行传送。 package cxf;import java.io.IOException;import java.io.StringReader;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import
使用SOAP UI查看请求参数,将请求参数以xml形式进行传送。
package cxf; import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; /** * httpclient调用webservice * * @author muyunfei * * <p>Modification History:</p> * <p>Date Author Description</p> * <p>------------------------------------------------------------------</p> * <p>Jan 11, 2017 牟云飞 新建</p> */ public class Client { public static void main(String[] args) { try { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost= new HttpPost("http://localhost:9090/****/services/CSGServiceService?wsdl"); //使用SOAP UI查看请求信息头部 String wsdlData="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.com/\">" +"<soapenv:Header/>" +"<soapenv:Body>" +"<ser:csg_message>" +"<!--Optional:-->" +"<data>"+"请求参数data"+"</data>" +"<!--Optional:-->" +"<token>"+"请求参数token"+"</token>" +"</ser:csg_message>" +"</soapenv:Body>" +"</soapenv:Envelope>"; // StringEntity myEntity = new StringEntity(wsdlData, // ContentType.create("application/soap+xml", "UTF-8")); StringEntity myEntity = new StringEntity(wsdlData, ContentType.create("text/xml", "UTF-8")); httpPost.setEntity(myEntity); //发送请求 ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse( final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); if(null!=entity){ String result= EntityUtils.toString(entity); return result; }else{ return null; } } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; //返回的json对象 String responseBody = httpclient.execute(httpPost, responseHandler); httpclient.close(); System.out.println(responseBody); //解析xml,获得return信息 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); StringReader sr = new StringReader(responseBody); InputSource is = new InputSource(sr); Document document = db.parse(is); Element root = document.getDocumentElement(); NodeList nodelist_return = root.getElementsByTagName("return"); String returnCont = nodelist_return.item(0).getTextContent(); //打印返回信息 System.out.println("webservice返回信息:"+returnCont); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }