Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

SAAJ Newbie

Thread view: 
writeameer@yahoo.com - 20 Feb 2007 12:04 GMT
Sorry for the dumb question but I need help. The following is a copy
of  Request.java from JavaEETutorial.pdf form the java website.
Except, instead of using an imaginary web site that didn't work
(wombat.ztrade.com), I tried to use a real one (webserviceX.net). I
get a java.util.NoSuchElementException.

Help Please !

import javax.xml.soap.*;
import javax.xml.namespace.QName;

import org.w3c.dom.NodeList;

import java.util.Iterator;
import java.net.URL;
public class Request {
    public static void main(String[] args)    {
        try {
            SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
            SOAPConnection connection =
soapConnectionFactory.createConnection();
            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage message = factory.createMessage();

            SOAPHeader header = message.getSOAPHeader();
            SOAPBody body = message.getSOAPBody();
            header.detachNode();

            QName bodyName = new QName("http://www.webserviceX.NET/",
"GetQuote","m");
            SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

            QName name = new QName("symbol");
            SOAPElement symbol = bodyElement.addChildElement(name);
            symbol.addTextNode("SUNW");

            URL endpoint = new URL ("http://www.webservicex.net/
stockquote.asmx");
            SOAPMessage response = connection.call(message, endpoint);
            connection.close();

            SOAPBody soapBody = response.getSOAPBody();

            Iterator iterator = soapBody.getChildElements(bodyName);
            bodyElement = (SOAPBodyElement)iterator.next();
            String lastPrice = bodyElement.getValue();

            System.out.print("The last price for SUNW is ");
            System.out.println(lastPrice);

        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
sdemchenko@gmail.com - 20 Feb 2007 13:27 GMT
1) Response.writeTo(System.out) will give a detailed error message.
You need to set mime-type:
message.getMimeHeaders().addHeader("SOAPAction", "http://
www.webserviceX.NET/GetQuote");
2) The service returns XML rather than price:
Iterator iterator = soapBody.getChildElements(new QName("http://
www.webserviceX.NET/", "GetQuoteResponse"));
bodyElement = (SOAPBodyElement)iterator.next();
String lastPrice = bodyElement.getTextContent();
writeameer@yahoo.com - 21 Feb 2007 09:17 GMT
Thank you for the help. I'm getting a SOAP response with "exception"
for GetQuoteResult for the code listed below:
How can I check I'm setting out the headers correctly in the request?

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Body>
        <GetQuoteResponse xmlns="http://www.webserviceX.NET/">
            <GetQuoteResult>exception</GetQuoteResult>
        </GetQuoteResponse>
    </soap:Body>
</soap:Envelope>

import javax.xml.soap.*;
import javax.xml.namespace.QName;

import org.w3c.dom.NodeList;

import java.util.Iterator;
import java.net.URL;
public class Request {
    public static void main(String[] args)    {
        try {
            SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
            SOAPConnection connection =
soapConnectionFactory.createConnection();
            MessageFactory factory = MessageFactory.newInstance();

            SOAPMessage message = factory.createMessage();
            SOAPHeader header = message.getSOAPHeader();
            SOAPBody body = message.getSOAPBody();
            header.detachNode();

            QName bodyName = new QName("http://www.webserviceX.NET/",
"GetQuote","m");
            SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

            QName name = new QName("symbol");
            SOAPElement symbol = bodyElement.addChildElement(name);
            symbol.addTextNode("SUNW");

            message.getMimeHeaders().addHeader("SOAPAction", "http://
www.webserviceX.NET/GetQuote");
            URL endpoint = new URL ("http://www.webservicex.net/
stockquote.asmx");
            SOAPMessage response = connection.call(message, endpoint);

            connection.close();

            SOAPBody soapBody = response.getSOAPBody();
            Iterator iterator = soapBody.getChildElements(new QName("http://
www.webserviceX.NET/", "GetQuoteResponse"));
            bodyElement = (SOAPBodyElement)iterator.next();
            String lastPrice = bodyElement.getTextContent();

            System.out.println(lastPrice);

            response.writeTo(System.out);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.