How to use IOS to invoke WebService (SOAP interface) _ios

Source: Internet
Author: User
Tags soap xmlns wsdl

In a project development process, the use of iOS to call the WebService interface, so take a short time to this aspect of the content for everyone to organize and share to everyone.

Method One: Using the WSDL2OBJC tool, turn the interface into an OC class.

1. Enter the WebService interface address in the browser (Safari is not available, I use Firefox), such as: Http://xxx.xxx.asmx, add after the address. WSDL is opened in HTTP://XXX.XXX.ASMX.WSDL.

2. Save the page as a WSDL file, save the suffix Plus. wsdl, and save it as a xxxxService.asmx.xml.wsdl.

3. Use the WSDL2OBJC tool to convert the WSDL file into an OC class.

4. Import all files of the generated OC class into the project. And then you can call the. Now do not write demo, I paste a part of the call code, later have the opportunity to send a demo.

 

Note: Above my webservice name is Murpxzshservice,soap is Murpxzshservicesoapbinding, the method that calls is Hkshlist.

Method Two: Call directly through network request, parse XML method.

Don't say, first code:

-(void) Viewdidappear: (BOOL) animated {[Super viewdidappear:animated]; If at this start request nsstring *webservicebodystr = [NSString stringWithFormat: @ "<jsjy_yjy xmlns=\" Http://mur Pcn.com/murpwebservice/\ ">" "<xxx>34192</xxx>" "<pagenow>1</pa Genow> "" <pagesize>20</pagesize> "</Jsjy_yjy>"];//here is the parameter Nsstri
                ng *webservicestr = [NSString stringWithFormat: @ "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n" "<soap:envelope xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ "xmlns:xsd=\" http://www.w3.org/2001/
                Xmlschema\ "xmlns:soap=\" http://schemas.xmlsoap.org/soap/envelope/\ ">\n" "<soap:body>\n" "%@\n" "</soap:body>\n" "</soap:Envelope>", WebService Bodystr];//webservice head NSString *soapactionstr = [Nsstring stringwithformat:@ "http://xxx.com/murpwebservice/%@", @ "Jsjy_yjy"];//soapaction nsurl *url = [NSURL URLWithString
  : @ "Http://xxxx/key/MurpjsjyService.asmx"];
  Nsmutableurlrequest *therequest = [Nsmutableurlrequest Requestwithurl:url];
  NSString *msglength = [NSString stringwithformat:@ "%ld", webservicestr.length];
  [Therequest addvalue:@ "Text/xml charset=utf-8" forhttpheaderfield:@ "Content-type"];
  [Therequest addvalue:soapactionstr forhttpheaderfield:@ "SOAPAction"];
  [Therequest addvalue:msglength forhttpheaderfield:@ "Content-length"];
  [Therequest sethttpmethod:@ "POST"];
  [Therequest Sethttpbody:[webservicestr datausingencoding:nsutf8stringencoding]];
  Nsurlconnection *theconn = [[Nsurlconnection alloc] initwithrequest:therequest delegate:self];
  if (theconn) {NSLog (@ "8888 hahaha laughs");
  }else {NSLog (@ "5555 you cry to Me"); 
 
}//Received data proxy-(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response {} -(void) connection:(nsurlconnection *) connection didreceivedata: (NSData *) data {nsstring *STR = [[NSString alloc] Initwithdata:data E
  Ncoding:nsutf8stringencoding]; NSLog (@ "%@", str)//Get the desired XML string and then parse the Nsxmlparser *par of the system itself = [[Nsxmlparser alloc] Initwithdata:[str Datausingenc
  Oding:nsutf8stringencoding]]; [Par setdelegate:self];//Set Nsxmlparser resolution method proxy [par parse];//call proxy resolves Nsxmlparser object to see if resolution is successful}//parse XML #pragma mark Xmlpa Rser//step 1: Preparing parsing-(void) Parserdidstartdocument: (Nsxmlparser *) parser{}//step 2: Preparing to parse node-(void) parser: (NSXMLP Arser *) Parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI qualifiedname: (NSString *) QName attributes: (nsdictionary *) attributedict{NSLog (@ "%@", Nsstringfromselector (_cmd));}//step 3: Get the content between the end and End nodes-(voi
 
d) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string{NSLog (@ "%@", string);} Step 4: Parse out the current node-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NsstrinG *) NamespaceURI QualifiedName: (NSString *) qname{NSLog (@ "%@", Nsstringfromselector (_cmd)); Step 5 parsing End-(void) Parserdidenddocument: (Nsxmlparser *) parser{}//Get CDATA block data-(void) Parser: (Nsxmlparser *) parser

 Foundcdata: (NSData *) cdatablock{}

The above is the way to use iOS to invoke WebService (SOAP interface), hoping to enlighten everyone.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.