|
楼主 |
发表于 2006-7-21 17:46:37
|
显示全部楼层
就是SOAP啦,我想自己手动写一个客户端程序,连接到已经规定好服务端上
以Google的API为例,我想不用Google的api,自己手动写一个满足google的wsdl文件规定的客户端程序,但是编写的时候就遇到上面的两个问题了,第一问题解决了一半,第二个问题毫无头绪,我把SAAJ的的API看了遍,但是就是没有看到怎么样添加不同类型的元素值,就看到了一个addTextNode(),但是google的wsdl里规定的还有boolean类型,还有int类型,不知道要怎么做,如果只用addTextNode()方法发出去,收到的就是类型错误的<soapfault>代码的回复
顺便贴一下我写的代码,很糙就是了
- import javax.xml.soap.*;
- import javax.xml.messaging.*;
- import javax.xml.namespace.QName;
- public class GoogleSearchClient{
- public static final String DEFAULT_HOST_URL = "http://api.google.com/search/beta2";
-
- public static void main(String[] args){
- try{
-
- // Create the connection
- SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
- SOAPConnection connection = scf.createConnection();
-
- // create a empty context message
- MessageFactory mf = MessageFactory.newInstance();
- SOAPMessage message = mf.createMessage();
-
- // create objects for the message
- SOAPPart soapPart = message.getSOAPPart();
- SOAPEnvelope envelope = soapPart.getEnvelope();
- SOAPBody body = envelope.getBody();
-
- //QName qname = new QName("urn:GoogleSearch","GoogleSearchPort");
- Name name = envelope.createName("doGoogleSearch","urn:GoogleSearch","GoogleSearchPort");
-
- SOAPElement bodyElement = body.addChildElement(name);
- // add message context of doGoogleSearch
- bodyElement.addChildElement("key").addTextNode("FKwUhSBQFHItwJcf5LxGsjKMyhC0cggp");
- bodyElement.addChildElement("q").addTextNode("Linux");
- bodyElement.addChildElement("start").addTextNode("0");
- bodyElement.addChildElement("maxResults").addTextNode("10");
- bodyElement.addChildElement("filter").addTextNode("false");
- bodyElement.addChildElement("restrict");
- bodyElement.addChildElement("safeSearch").addTextNode("false");
- bodyElement.addChildElement("lr");
- bodyElement.addChildElement("ie");
- bodyElement.addChildElement("oe");
-
- message.saveChanges();
-
- // Output the message
- System.out.println("message:");
- message.writeTo(System.out);
- System.out.println();
-
- URLEndpoint url = new URLEndpoint(DEFAULT_HOST_URL);
- SOAPMessage reply = connection.call(message, url);
-
- // check the response
- System.out.println("the response");
- reply.writeTo(System.out);
-
- connection.close();
- }catch(Throwable e){
- e.printStackTrace();
- }
- }
- }
复制代码
还有就是google的wsdl文件,http://api.google.com/GoogleSearch.wsdl
|
|