81,094
社区成员
发帖
与我相关
我的任务
分享
public class TestConn {
private String ns = "http://tempurl.org";
private String wsdlUrl = "http://180.168.105.214:8080/TTPOS/sales.asmx?WSDL";
@Test
public void test01() {
try {
/**
* 拼接对应的soap;
*/
//1、创建消息工厂
MessageFactory factory = MessageFactory.newInstance();
//2、根据消息工厂创建SoapMessage
SOAPMessage message = factory.createMessage();
//3、创建SOAPPart
SOAPPart part = message.getSOAPPart();
//4、获取SOAPENvelope
SOAPEnvelope envelope = part.getEnvelope();
//5、可以通过SoapEnvelope有效的获取相应的Body和Header等信息
SOAPBody body = envelope.getBody();
//6、根据Qname创建相应的节点(QName就是一个带有命名空间的)
// QName qname = new QName("http://tempurl.org",
// "test_a_plus_b");//<ns:add xmlns="http://java.zttc.edu.cn/webservice"/>
QName qname= new QName(XMLConstants.NULL_NS_URI,"q0:test_a_plus_b","q0");
SOAPBodyElement ele = body.addBodyElement(qname);
ele.addChildElement("q0:al_value1").setValue("22");
ele.addChildElement("q0:al_value2").setValue("33");
//打印消息信息
System.out.println("soap创建成功!");
message.writeTo(System.out);
System.out.println("\n next....");
/**
* 传输soap到服务器
*/
//1、创建服务(Service)
URL url = new URL(wsdlUrl);
QName sname = new QName(ns,"MyServiceImplService");
Service service = Service.create(url,sname);
System.out.println("--s");
//2、创建Dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"),
SOAPMessage.class, Service.Mode.MESSAGE);
System.out.println("d");
//4、创建QName来指定消息中传递数据
// QName ename = new QName(ns,"add","nn");//<nn:add xmlns="xx"/>
message.writeTo(System.out);
System.out.println("\n invoking.....");
System.out.println("p");
//5、通过Dispatch传递消息,会返回响应消息
SOAPMessage response = dispatch.invoke(message);
response.writeTo(System.out);
System.out.println();
/**
* 解析返回的内容
*/
//将响应的消息转换为dom对象
Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
String str = doc.getElementsByTagName("addResult").item(0).getTextContent();
System.out.println(str);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public class TestConn {
private String ns = "http://tempurl.org";
private String wsdlUrl = "http://180.168.105.214:8080/TTPOS/sales.asmx?WSDL";
@Test
public void test01() {
try {
//1、创建消息工厂
MessageFactory factory = MessageFactory.newInstance();
//2、根据消息工厂创建SoapMessage
SOAPMessage message = factory.createMessage();
//3、创建SOAPPart
SOAPPart part = message.getSOAPPart();
//4、获取SOAPENvelope
SOAPEnvelope envelope = part.getEnvelope();
//5、可以通过SoapEnvelope有效的获取相应的Body和Header等信息
SOAPBody body = envelope.getBody();
//6、根据Qname创建相应的节点(QName就是一个带有命名空间的)
QName qname = new QName("http://tempurl.org",
"test_a_plus_b");//<ns:add xmlns="http://java.zttc.edu.cn/webservice"/>
//如果使用以下方式进行设置,会见<>转换为<和>
//body.addBodyElement(qname).setValue("<a>1</a><b>2</b>");
SOAPBodyElement ele = body.addBodyElement(qname);
ele.addChildElement("al_value1").setValue("22");
ele.addChildElement("al_value2").setValue("33");
//打印消息信息
message.writeTo(System.out);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[/quote]
打印出来是这样,和要求的格式不一样,我也不知道怎么传到服务器
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<test_a_plus_b xmlns="http://tempurl.org">
<al_value1 xmlns="">22</al_value1>
<al_value2 xmlns="">33</al_value2>
</test_a_plus_b></soapenv:Body>
</soapenv:Envelope>
public class TestConn {
private String ns = "http://tempurl.org";
private String wsdlUrl = "http://180.168.105.214:8080/TTPOS/sales.asmx?WSDL";
@Test
public void test01() {
try {
//1、创建消息工厂
MessageFactory factory = MessageFactory.newInstance();
//2、根据消息工厂创建SoapMessage
SOAPMessage message = factory.createMessage();
//3、创建SOAPPart
SOAPPart part = message.getSOAPPart();
//4、获取SOAPENvelope
SOAPEnvelope envelope = part.getEnvelope();
//5、可以通过SoapEnvelope有效的获取相应的Body和Header等信息
SOAPBody body = envelope.getBody();
//6、根据Qname创建相应的节点(QName就是一个带有命名空间的)
QName qname = new QName("http://tempurl.org",
"test_a_plus_b");//<ns:add xmlns="http://java.zttc.edu.cn/webservice"/>
//如果使用以下方式进行设置,会见<>转换为<和>
//body.addBodyElement(qname).setValue("<a>1</a><b>2</b>");
SOAPBodyElement ele = body.addBodyElement(qname);
ele.addChildElement("al_value1").setValue("22");
ele.addChildElement("al_value2").setValue("33");
//打印消息信息
message.writeTo(System.out);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.client;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
@WebService(name = "salesSoap", targetNamespace = "http://tempurl.org")
public interface SalesSoap {
@WebMethod(operationName = "test_a_plus_b", action = "http://tempurl.org/test_a_plus_b")
@WebResult(name = "test_a_plus_bResult", targetNamespace = "http://tempurl.org")
@RequestWrapper(localName = "test_a_plus_b", targetNamespace = "http://tempurl.org", className = "com.client.TestAPlusB")
@ResponseWrapper(localName = "test_a_plus_bResponse", targetNamespace = "http://tempurl.org", className = "com.client.TestAPlusBResponse")
public int testAPlusB(
@WebParam(name = "al_value1", targetNamespace = "http://tempurl.org") int alValue1,
@WebParam(name = "al_value2", targetNamespace = "http://tempurl.org") int alValue2);
}
package com.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class MyClient extends Service {
protected MyClient(URL wsdlDocumentLocation, QName serviceName) {
super(wsdlDocumentLocation, serviceName);
}
public static void main(String[] args) {
try {
URL url = new URL("http://180.168.105.214:8080/TTPOS/sales.asmx?wsdl");
QName serviceName = new QName("http://tempurl.org", "sales");
QName portName = new QName("http://tempurl.org", "salesSoap");
MyClient mc = new MyClient(url, serviceName);
SalesSoap remote = mc.getPort(portName, SalesSoap.class);
System.out.println(remote.testAPlusB(123, 456));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
declare
request utl_http.req;
response utl_http.resp;
methodName varchar2(99) := 'test_a_plus_b';
rev_pack varchar2(99):='tempurl.org';
v_prefix varchar2(99):= 'soap';
reqxmlstr varchar2(4000):=
'<'||v_prefix||':Envelope xmlns:'||v_prefix||'="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<'||v_prefix||':Body>
<'||methodName||' xmlns="http://'||rev_pack||'" >
<al_value1>123</al_value1>
<al_value2>234</al_value2>
</'||methodName||'>
</'||v_prefix||':Body>
</'||v_prefix||':Envelope>';
WSDLURL varchar2(99) :=
'http://180.168.105.214:8080/TTPOS/sales.asmx';
--去掉?wdsl后的请求地址
respxmlstr varchar2(4000):='';
XML xmltype;
XML1 xmltype;
String varchar2(9999);
begin
request := utl_http.begin_request(WSDLURL,'POST',utl_http.HTTP_VERSION_1_1);
utl_http.set_header(request, 'Content-Type', 'text/xml;charset=utf-8');
utl_http.set_header(request, 'Content-Length', length(reqxmlstr));
utl_http.write_text(request, reqxmlstr);
response := utl_http.get_response(request);
utl_http.read_text(response, respxmlstr);
dbms_output.put_line('--返回--');
dbms_output.put_line(respxmlstr);
utl_http.end_response(response);
end;