Objective:
Currently our project team is still using WebService this HTTP way, and some Web services provided by the external interface is still in the use of WebService way, so summed up the writing this article.
Take soap1.2 's request as an example, in the example we pass in the user name and password to the service, the service returns an XML data.
First, let's open up the request of soap1.2.
WSDL, example: Orderapp.asmxpost/******app.asmx http/1.1//here fill in the service address host:100.100.100.100content-type:application/soap+ xml Charset=utf-8content-length:length<?xml version= "1.0" encoding= "Utf-8"?><soap12:envelope xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" Xmlns:xsd= "http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05 /soap-envelope "> <soap12:body> <Login xmlns="My Project"> <UserName>String</UserName> <PassWord>String</PassWord> </Login> </soap12:body></soap12:envelope>
Next, we'll stitch the request body in the code:
/** * Arg1 is the first parameter key, ARG2 is the first parameter value, ARG3 is the second parameter key, ARG4 is the second parameter value, *method is the method name, and xmlns is the namespace * / Public void Initsoap(String arg1,string arg2,string arg3,string arg4,string method,string xmlns) {String Soaprequestdata ="<?xml version=\" 1.0\ "encoding=\" utf-8\ "?> "+" <soap12:envelope xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ ""+" xmlns:xsd=\" Http://www.w3.org/2001/xmlschema\ ""+"xmlns:soap12=\" http://www.w3.org/2003/05/soap-envelope\ "> "+"<soap12:Body>"+"<"+method+""+"xmlns="+"\""+xmlns+"\""//+ "<login xmlns=\" my Project \ ">"+"<"+arg1+">"+arg2+"</"+arg1+">"+"<"+arg3+">"+arg4+"</"+arg3+">"//+ "<UserName>" + "YQPIS0670" + "</UserName>"//+ "<PassWord>" + "YQPIS0670" + "</PassWord>"+"</Login>"+"</soap12:Body>"+"</soap12:Envelope>"; }
The second step is to open the thread and perform the access
NewThread (NewRunnable () {@Override Public void Run() {//TODO auto-generated method stubPostmethod Postmethod =NewPostmethod ("service address, which is the host+ port number in request above +post");///Then add the SOAP request data to the Postmethod byte[] B =NULL;Try{b = soaprequestdata.getbytes ("Utf-8"); }Catch(Unsupportedencodingexception e) {//TODO auto-generated catch blockE.printstacktrace (); } InputStream is=NewBytearrayinputstream (b,0, b.length); Requestentity re =NewInputstreamrequestentity ( is, B.length,"Application/soap+xml; Charset=utf-8 "); Postmethod.setrequestentity (re);//Last Generate a HttpClient object and issue a Postmethod requestHttpClient HttpClient =NewHttpClient ();Try{intStatusCode = Httpclient.executemethod (Postmethod);if(StatusCode = = $) {LOG.D ("Soaprequestdata","The Call succeeded!" "); StringBuffer buffer =NewStringBuffer ();//Parser factory classDocumentbuilderfactory dbf = Documentbuilderfactory.newinstance (); Documentbuilder db = Dbf.newdocumentbuilder ();//Return streaming dataInputStream soapresponsedata = Postmethod. Getresponsebodyasstream (); Document DM = Db.parse (soapresponsedata);//element and node are the same concept //Different element provides more methods if(Dm.getelementsbytagname ("Root"). Item (0). Getfirstchild ()! =NULL) {//J is root is the number of nodes below the root node for(intj =0; J < DM. getElementsByTagName ("Root"). Item (0). Getchildnodes (). GetLength (); J + +) {String RESULT3 = Dm.getelementsbytagname ("Root"). Item (0). Getchildnodes (). Item (j). Gettextcontent (); Buffer.append (RESULT3); } } }Else{LOG.D ("Soaprequestdata","The call failed! Error code: "+ StatusCode); } }Catch(HttpException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(Parserconfigurationexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(Saxexception e) {//TODO auto-generated catch blockE.printstacktrace (); }}). Start ();
Everyone can look at my XML data:
<Root><Result>1</Result><Message>Login successful</Message><memberidcard>YQPIS0670</memberidcard><UserName>Qi Hongtao</UserName><Birthday>2010-11-04</Birthday><Photo>...</Photo><telephone/><memberstate>Current members</memberstate><memberstatus>Friendship Membership</memberstatus><membersex>12ee640d-a037-497e-966e-91fc2186c8b4</membersex><nationality>175f0624-29d1-4b88-9d97-d72ebb1e6a1c</Nationality><membersexmemo>Man</Membersexmemo><nationalitymemo>China</Nationalitymemo></Root>
If there are multiple nodes, you can modify Dm.getelementsbytagname ("Root"). Item (0). Getchildnodes (). Item (j). Gettextcontent ()
Is Dm.getelementsbytagname ("Root"). Item (0). Getchildnodes (). Item (j). Getchildnodes (). Item (k). Gettextcontent (); is the property value of the 3-tier node.
Well, if you do not understand, please contact me oh;
Teach you soap access WebService and Dom parse the returned XML data