在我们做项目的时候,往往移动客户端是现有平台的一个扩展,因此客户端实现往受限于服务采用的技术。那么目前大多数在一个新项目开始的时候,可能会考虑如果要考虑移动客户端的话会提供接口,但是,有些时候服务端已经有接口,而且这个接口可能不仅仅是提供给移动平台使用的,比如它的实现采用的是SOAP协议的WebServices实现。那么这个时候iOS端应该要如何去做呢?
//1. 创建URL
NSURL *url = [NSURL URLWithString:@"http://www.joy11.com/WebService/LiAnService.asmx"];
//2. 创建请求(post方式创建可变请求)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//3. 指定请求为post方式
[request setHTTPMethod:@"POST"];
//4. 准备提交的数据
NSString *soapMessage = [NSString stringWithFormat:
@"<?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:Header>"
"<AuthSoapHd xmlns=\"http://tempuri.org/\">"
"<strUserName>lian2014</strUserName>" //填写soap herder账户
"<strPassword>lian0403</strPassword>" //填写soap herder密码
"</AuthSoapHd>"
"</soap12:Header>"
"<soap12:Body>"
"<LoginLink xmlns=\"http://tempuri.org/\">"
"<key>%@</key>" //填写用户Id
"</LoginLink>"
"</soap12:Body>"
"</soap12:Envelope>", [AccountTool sharedAccountTool].currentAccount.uid];
NSURL *url1 = [NSURL URLWithString:@"http://www.joy11.com/webservice/lianservice.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url1];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/LoginLink" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
代码为直接lain'jie