IOS手机端应用程序调用WebService

葛磊
2023-12-01
手机端IOS应用程序调用WebService(JAVA)代码如下:

NSString *soapMessage = [NSString stringWithFormat:
            @"<v:Envelope xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" xmlns:c=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:v=\"http://schemas.xmlsoap.org/soap/envelope/\"><v:Header /><v:Body><getUserInfo xmlns=\"http://IP:端口/MobileInfo/services/Info/\" id=\"o0\" c:root=\"1\"><mobile i:type=\"d:string\">%@</mobile><coNumber i:null=\"true\" /><type i:type=\"d:string\">%@</type><vCode i:type=\"d:string\">%@</vCode><isComplete i:type=\"d:string\">%@</isComplete></getUserInfo></v:Body></v:Envelope>",txtPhone.text,@"0",txtVerificationCode.text,@"0"
            ];
NSURL *url = [NSURL URLWithString:@"http://域名:端口/MobileInfo/services/Info?wsdl"];
            //请求发送到的路径
            ASIHTTPRequest *theRequest =  [ASIHTTPRequest requestWithURL:url];
            NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
            [theRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
            [theRequest addRequestHeader: @"SOAPAction" value:@"http://域名:端口/MobileInfo/services/Info?wsdl"];
            [theRequest addRequestHeader:@"Content-Length" value:msgLength];
            [theRequest setRequestMethod:@"POST"];
            [theRequest appendPostData: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
            [theRequest setDefaultResponseEncoding:NSUTF8StringEncoding];
            [theRequest startSynchronous];
            NSError *error = [theRequest error];
            if (!error) {
            NSData *data = [theRequest responseData];//WebService接口返回的数据
            NSString *theXML = [[NSString alloc] initWithBytes: [data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding];//将返回数据转换为字符串,进行解析(本文中返回的数据为XML数据)
                    NSLog(@"%@",theXML);
            }

 

ASIHTTPRequest 为ASIHTTPRequest开源项目提供的类,开源源码可网上下载,也可在本文中下载。zip 是在Mac下生成的压缩包,rar是在windows xp下生成的压缩包,选择一个适合你电脑操作系统的下载,解压缩后加入项目中即可使用。

 类似资料: