Java
Chilkat2 Java
HTTP SOAP 1.2 Request and Response using POST
See more HTTP Examples
Demonstrates a working SOAP 1.2 request and response using POST with a live server. You may try running this example with the URLs and data provided. See http://wsf.cdyne.com/WeatherWS/Weather.asmx?op=GetCityWeatherByZIP for details.Note: This example is correct in theory, but no longer works for live testing because the SOAP service provider (cdyne.com) has made changes or discontinued the free service.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.Xml;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// --------------------------------------------------------------------------------
// Also see Chilkat's Online WSDL Code Generator
// to generate code and SOAP Request and Response XML for each operation in a WSDL.
// --------------------------------------------------------------------------------
Http http = new Http();
Xml soapXml = new Xml();
soapXml.setTag("soap12:Envelope");
soapXml.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
soapXml.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
soapXml.addAttribute("xmlns:soap12", "http://www.w3.org/2003/05/soap-envelope");
soapXml.newChild2("soap12:Body", "");
soapXml.getChild2(0);
soapXml.newChild2("GetCityWeatherByZIP", "");
soapXml.getChild2(0);
soapXml.addAttribute("xmlns", "http://ws.cdyne.com/WeatherWS/");
soapXml.newChild2("ZIP", "60187");
soapXml.getRoot2();
System.out.println(soapXml.getXml());
HttpRequest req = new HttpRequest();
req.setHttpVerb("POST");
req.setSendCharset(false);
req.addHeader("Content-Type", "application/soap+xml; charset=utf-8");
req.addHeader("SOAPAction", "http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP");
req.setPath("/WeatherWS/Weather.asmx");
req.loadBodyFromString(soapXml.getXml(), "utf-8");
http.setFollowRedirects(true);
HttpResponse resp = new HttpResponse();
try {
http.httpSReq("wsf.cdyne.com", 80, false, req, resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Xml xmlResponse = new Xml();
xmlResponse.loadXml(resp.getBodyStr());
System.out.println(xmlResponse.getXml());
}
}