Commit ba401de1 by Patryk Czarnik

Klient8_Dispatch_SoapMessage

parent bb3f718c
package ogloszenia.klient_niskopoziomowy;
import jakarta.xml.soap.*;
import jakarta.xml.ws.Dispatch;
import jakarta.xml.ws.Service;
import org.w3c.dom.NodeList;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
public class Klient8_Dispatch_SoapMessage {
public static final String NS_URI = "http://soap.ogloszenia/";
public static void main(String[] args) {
System.out.println("Startujemy...");
int szukaneId = 3;
try {
URL wsdlUrl = new URL("http://localhost:8080/OgloszeniaSerwer/Ogloszenia?wsdl");
QName serviceName = new QName(NS_URI, "OgloszeniaService");
QName portName = new QName(NS_URI, "OgloszeniaPort");
MessageFactory mf = MessageFactory.newInstance();
Service service = Service.create(wsdlUrl, serviceName);
Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
System.out.println("Mam Dispatchera " + dispatch);
SOAPMessage request = mf.createMessage();
SOAPBody requestBody = request.getSOAPBody();
SOAPBodyElement readOne = requestBody.addBodyElement(new QName(NS_URI, "readOne", "o"));
SOAPElement parametr = readOne.addChildElement(new QName("id"));
parametr.setTextContent(String.valueOf(szukaneId));
request.writeTo(System.out);
System.out.println("\n\nWysyłam zapytanie");
SOAPMessage result = dispatch.invoke(request);
System.out.println("Mam wynik: " + result);
result.writeTo(System.out);
SOAPBody resultBody = result.getSOAPBody();
NodeList znalezione = resultBody.getElementsByTagNameNS(XMLConstants.NULL_NS_URI, "cena");
if(znalezione.getLength() > 0) {
System.out.println("\n\nCena: " + znalezione.item(0).getTextContent());
}
System.out.println("Gotowe");
} catch (SOAPException | IOException e) {
throw new RuntimeException(e);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment