IOS calls WebService (SOAP Interface) method, webservicesoap

Source: Internet
Author: User

IOS calls WebService (SOAP Interface) method, webservicesoap

Recently developed, I used iOS to call the webService interface. I haven't done it before. It's a little troublesome. It took a long time to get it done. Now I 've sorted out the available methods and shared them with you, I hope to communicate with you.

Method 1:

Use the WSDL2ObjC tool to convert interfaces into OC classes.

1. Enter the webService interface address in the browser (Safari is not available, I use Firefox), such as: http://xxx.xxx.asmx, the address is added to the. wsdl to open the http://xxx.xxx.asmx.wsdl.

2. Save the page as a wsdl file. Add the. wsdl extension when saving the page and save it as xxxxService. asmx. xml. wsdl.

3. Use the WSDL2ObjC tool to convert the wsdl file into an OC class.

4. Import all generated OC class files to the project. Then you can call it. If I have not written a Demo, I will post a part of the call code, and I will have the opportunity to upload a Demo later.

-(NSString *) protocol :( NSInteger) aPagenow pagesize :( NSInteger) aPagesize {MURPXzshServiceSoapBinding * binding = [[using alloc] initWithAddress: [NSString stringWithFormat: @ "% @", self. jjjj, XZSH_SERVICE]; // interface address binding. logXMLInOut = YES; // you can directly copy MURPXzshService_HkshList * parm = [[MURPXzshService_HkshList alloc] init]; // The method in the interface initializes parm. xxxx = [umcid stringValue]; // The parm parameter in the interface. pagenow = [NSNumber numberWithInteger: aPagenow]; // The parm parameter in the interface. pagesize = [NSNumber numberWithInteger: aPagesize]; // The method parameter MURPXzshServiceSoapBindingResponse * resp = [binding HkshListUsingParameters: parm] in the interface; // call method // The following is the returned value for (id mine in resp. bodyParts) {if ([mine isKindOfClass: [MURPXzshService_HkshListResponse class]) {NSString * resultStr = [mine HkshListResult]; return resultStr ;}} return nil ;}

Note: My webService name is MURPXzshService, and Soap is MURPXzshServiceSoapBinding. The method of calling this service is HkshList.

 

Method 2:

The XML parsing method is called directly through network requests.

If you don't want to talk about it, go to the code first:

-(Void) viewDidAppear :( BOOL) animated {[super viewDidAppear: animated]; // if you start requesting NSString * webServiceBodyStr = [NSString stringWithFormat: @ "<Jsjy_yjy xmlns = \" http://murpcn.com/murpwebservice/\ ">" "<xxx> 34192 </xxx>" "<pagenow> 1 </pagenow>" <pagesize> 20 </pagesize> "" </Jsjy_yjy> "]; // here is the NSString * webServiceStr = [NSString stringWithFormat: @ "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> \ N "" <soap: Envelope xmlns: xsi = \ "external" xmlns: xsd = \ "http://www.w3.org/2001/XMLSchema\" xmlns: soap = \ "external"> \ n "" <soap: body> \ n "" % @ \ n "" </soap: Body> \ n "" </soap: Envelope> ", webServiceBodyStr]; // webService header 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: callback]; NSURLConnection * theConn = [[NSURLConnection alloc] initWithRequest: theRequest delegate: self]; if (theConn) {NSLog (@ "8888 hahaha ");} else {NSLog (@ "5555 cry for me ");}}

// The proxy that receives the data

-(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response {}-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {NSString * str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "% @", str ); // obtain the desired XML string and parse it. // NSXMLParser * par = [[NSXMLParser alloc] initWithData: [str dataUsingEncoding: NSUTF8StringEncoding]; [par setDelegate: self]; // set the NSXMLParser object resolution method proxy [par parse]; // call the proxy to parse the NSXMLParser object to see if the resolution is successful}

// Parse XML

# Pragma mark xmlparser // step 1: Prepare for parsing-(void) parserDidStartDocument :( NSXMLParser *) parser {} // step 2: Prepare the parsing node-(void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName attributes :( NSDictionary *) attributeDict {NSLog (@ "% @", NSStringFromSelector (_ cmd);} // step 3: Get content between the first and last nodes-(void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {NSLog (@ "% @", string) ;}// step 4: parse the current node-(void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName namespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {NSLog (@ "% @", NSStringFromSelector (_ cmd);} // step 5; resolution end-(void) parserDidEndDocument :( NSXMLParser *) parser {} // obtain cdata block data-(void) parser :( NSXMLParser *) parser foundCDATA :( NSData *) CDATABlock {}

The request is probably like this.

I cannot find a good way to write the parameters in the request header. Please advise me if you have a better solution.

 

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.