webService 编程

来源:互联网 发布:sql当没有用exists 编辑:程序博客网 时间:2024/05/29 07:12

这段时间,一直都在搞 webService 编程和 socket 编程,搞的死去活来的。总算是有点了解和认识,现在作笔录:<p>一,webService源代码</p>        NSString *url = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><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/\"><soap:Body><%@ xmlns=\"http://www.zgbeidou.cn/\"><username>%@</username><password>%@</password><servicepassword>YFGPS2008</servicepassword></%@></soap:Body></soap:Envelope>",type,nil,nil,type]; NSString *urlLength = [NSString stringWithFormat:@"%d",[url length]]; [urlRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [urlRequest addValue:urlLength forHTTPHeaderField:@"Content-Length"]; NSString *SOAPAction_str = [NSString stringWithFormat:@"http://www.zgbeidou.cn/%@",type]; [urlRequest addValue:SOAPAction_str forHTTPHeaderField:@"SOAPAction"]; [urlRequest setHTTPMethod:@"POST"]; [urlRequest setHTTPBody:[url dataUsingEncoding:NSUTF8StringEncoding]]; //请求并接收返回的数据  NSURLResponse *reponse; NSError *error = nil; NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&reponse error:&error]; NSMutableString *resultString = [[NSMutableString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]; //    NSLog(@"urlRequest = %@",resultString); //解析xml数据 GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:responseData options:0 error:nil]; GDataXMLElement *rootElement = [doc rootElement];     NSData *data = [[rootElement stringValue]dataUsingEncoding:NSUTF8StringEncoding]; NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"arr11111 = %@",arr ); 




0 0
原创粉丝点击