Ciao,
sto sviluppando un client di un web service più o meno in questo modo:
E quello che mi chiedo è: ma se io devo connettermi utilizzando username e password? coem posso fare?Codice PHP:import javax.xml.soap.*;
import java.util.*;
import java.net.URL;
public class Request {
public static void main(String[] args)
{
try
{
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPFactory soapFactory = SOAPFactory.newInstance();
MessageFactory factory =MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
//rimuovo questo nodo dall'alberatura header.detachNode();
//oppure header.addHeaderElement...
SOAPBody body = message.getSOAPBody();
Name bodyName = soapFactory.createName("GetLastTradePrice", "m","http://wombats.ztrade.com");
SOAPBodyElement bodyElement =body.addBodyElement(bodyName);
Name name = soapFactory.createName("symbol");
SOAPElement symbol =bodyElement.addChildElement(name); symbol.addTextNode("SUNW");
URL endpoint = new URL("http://wombat.ztrade.com/quotes");
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(); }
}
}
Google-ando ho trovato questo metodo ma non mi convince.. c'è qualcosa di migliore?
Grazie!codice:Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( username, password.toCharArray()); } });![]()

Rispondi quotando