java - How to create SOAP Request , the easy way -
i new soap,
hardcoding soap request , sending server,its working fine.now want create soap request dynamically, new soap , have no idea prepare xml use jaxb , should use same creating soap request or there better approach?
my java code
public class test { public static void main(string[] args) { string xml ="<soapenv:envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n" + " <soapenv:header/>\n" + " <soapenv:body>\n" + " <tem:requestdata>\n" + " <!--optional:-->\n" + " <tem:requestdocument>\n" + "\n" + "\n" + "<![cdata[ <request>\n" + "<authentication cmid='68' guid='5594fb83-f4d4-431f-b3c5-ea6d7a8ba795' password='poihg321tr' function='1' />\n" + "<establishment id='4297867' >\n" + "</establishment>\n" + "</request> ]]> \n" + "\n" + "\n" + "</tem:requestdocument>\n" + " </tem:requestdata>\n" + " </soapenv:body>\n" + "</soapenv:envelope>"; sendrequesttochannel(xml); } private static string sendrequesttochannel(string request) { string xmldata = null; bufferedreader rd = null; bufferedwriter bw = null; string line = null; string linesep = null; string data = null; stringbuffer serverdata = null; int socket_timeout = 6000, port = 80; string host = null,path=null; try { java.net.url url = new java.net.url("http://pp.hotels.travelrepublic.co.uk/channelmanager.svc"); host=url.gethost(); path=url.getpath(); socket clisocket = new socket(); clisocket.connect(new inetsocketaddress(host, port), socket_timeout); bw = new bufferedwriter(new outputstreamwriter(clisocket.getoutputstream())); bw.write("post " + path + " http/1.0"); bw.write("\r\n"); bw.write("accept-encoding: gzip,deflate"); bw.write("\r\n"); bw.write("content-type: text/xml;charset=utf-8"); bw.write("\r\n"); bw.write("soapaction: http://tempuri.org/ipublicchannelmanagerservice/requestdata"); bw.write("\r\n"); bw.write("content-length: " + request.length() + "\r\n"); bw.write("host: " + host + "\r\n"); bw.write("proxy-connection: keep-alive\r\n"); bw.write("user-agent: apache-httpclient/4.1.1 (java 1.5)\r\n"); bw.write("\r\n"); bw.write(request); bw.flush(); rd = new bufferedreader(new inputstreamreader(clisocket.getinputstream())); serverdata = new stringbuffer(""); linesep = system.getproperty("line.separator"); while ((line = rd.readline()) != null) { serverdata.append(line); serverdata.append(linesep); } data = serverdata.tostring(); int index = data.indexof("<"); if (index != -1) { xmldata = data.substring(index); system.out.println("\r\n \r\n xml data \r\n "+xmldata); } else { system.out.println("\r\n \r\n xml data not retrived"); } } catch (java.net.unknownhostexception uh) { uh.printstacktrace(); system.out.println("$$$$$$$$$$$$ in sendrequesttochannel : unknownhostexception " + uh.getmessage()); return " in sendrequesttochannel : unknownhostexception " + uh.tostring(); } catch (ioexception ioe) { ioe.printstacktrace(); system.out.println("$$$$$$$$$$$$ in sendrequesttochannel : ioexception " + ioe.getmessage()); return " in sendrequesttochannel : ioexception " + ioe.tostring(); } catch (exception e) { e.printstacktrace(); system.out.println("$$$$$$$$$$$$ in sendrequesttochannel : exception " + e.getmessage()); return " in sendrequesttochannel : exception " + e.tostring(); } { try { if (bw != null) { bw.close(); } } catch (ioexception ex) { } try { if (rd != null) { rd.close(); } } catch (ioexception ex) { } bw = null; rd = null; line = null; linesep = null; data = null; serverdata = null; } return xmldata; } }
there multiple frameworks available this
please see http://www.javabeat.net/programming-web-services-using-apache-axis/
Comments
Post a Comment