iPhone實現(xiàn)XML的POST傳遞并接收數(shù)據(jù)
作者:佚名
本文介紹對是iPhone實現(xiàn)XML的POST傳遞并接收數(shù)據(jù),主要是向服務器端傳遞一個xml數(shù)據(jù)流,服務器端接受數(shù)據(jù)并返回一個xml數(shù)據(jù)類型。
iPhone實現(xiàn)XML的POST傳遞并接收數(shù)據(jù)是本文要介紹的內容,文章是基于代碼實現(xiàn),在iphone開發(fā)中,經常會遇到和服務器端交互數(shù)據(jù),下面代碼是向服務器端傳遞一個xml數(shù)據(jù)流,服務器端接受數(shù)據(jù)并返回一個xml數(shù)據(jù)類型。
iphone傳遞數(shù)據(jù)的代碼:
- -(void) postxml
- {
- //prepar request
- NSString *urlString = [NSString stringWithFormat:@"path"];
- NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
- [request setURL:[NSURL URLWithString:urlString]];
- [request setHTTPMethod:@"POST"];
- //set headers
- NSString *contentType = [NSString stringWithFormat:@"text/xml"];
- [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
- //create the body
- NSMutableData *postBody = [NSMutableData data];
- [postBody appendData:[[NSString stringWithFormat:@"<Request Action=\"Login\">"] dataUsingEncoding:NSUTF8StringEncoding]];
- [postBody appendData:[[NSString stringWithFormat:@"<Body>"] dataUsingEncoding:NSUTF8StringEncoding]];
- [postBody appendData:[[NSString stringWithFormat:@"<Username>wangjun</Username>"] dataUsingEncoding:NSUTF8StringEncoding]];
- [postBody appendData:[[NSString stringWithFormat:@"<Password>password</Password>"] dataUsingEncoding:NSUTF8StringEncoding]];
- [postBody appendData:[[NSString stringWithFormat:@"<PlatformID>2</PlatformID>"] dataUsingEncoding:NSUTF8StringEncoding]];
- [postBody appendData:[[NSString stringWithFormat:@"<PlatformVersion>3.1.3</PlatformVersion>"] dataUsingEncoding:NSUTF8StringEncoding]];
- [postBody appendData:[[NSString stringWithFormat:@"<TaskViewerName>IP 1.3</TaskViewerName>"] dataUsingEncoding:NSUTF8StringEncoding]];
- [postBody appendData:[[NSString stringWithFormat:@"<TaskViewerVersion>3</TaskViewerVersion>"] dataUsingEncoding:NSUTF8StringEncoding]];
- [postBody appendData:[[NSString stringWithFormat:@"</Body>"] dataUsingEncoding:NSUTF8StringEncoding]];
- [postBody appendData:[[NSString stringWithFormat:@"</Request>"] dataUsingEncoding:NSUTF8StringEncoding]];
- //post
- [request setHTTPBody:postBody];
- //get response
- NSHTTPURLResponse* urlResponse = nil;
- NSError *error = [[NSError alloc] init];
- NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
- NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
- NSLog(@"Response Code: %d", [urlResponse statusCode]);
- if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
- NSLog(@"Response: %@", result);
- }
- }
運行結果是:
- 2010-09-09 13:19:25.190 PostXmlDemo1[685:307] 點擊我了,哈哈
- 2010-09-09 13:19:28.450 PostXmlDemo1[685:307] Response Code: 200
- 2010-09-09 13:19:28.456 PostXmlDemo1[685:307] Response:
- <Response Action=’Login’><Cookie>DFGHJYUIILNBV$%^&*gghj^&IUF%^R^DFYUIHNVB
- </Cookie><Body><User Name=’用戶名’ >< Avatar>登陸用戶頭像URL</Avatar></User><State>1</State><Description>
- 登陸成功后的描述信息。</Description></Body></Response>
小結
iPhone實現(xiàn)XML的POST傳遞并接收數(shù)據(jù)的內容介紹完了,希望本文對你有所幫助。
本文來自:>http://wangjun.easymorse.com/?p=864
責任編輯:zhaolei
來源:
互聯(lián)網