During iPhone development, data is often exchanged with the server. The following code transmits an XML data stream to the server. The server accepts the data and returns an XML data type.
Code for iPhone data transmission:
-(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 );
}
}
The running result is:
13:19:25. 190 postxmldemo1 [685: 307] Click me, haha
13:19:28. 450 postxmldemo1 [685: 307] response code: 200
13:19:28. 456 postxmldemo1 [685: 307] response: <Response Action = 'login'> <cookie> dfghjyuiilnbv $ % ^ & * gghj ^ & IUF % ^ r ^ dfyuihnvb </cookie> <body> <user name = 'username'> <Avatar> User Profile URL </Avatar> </user> <State> 1 </State> <description> description after successful logon. </Description> </body> </response>
IPhone source code: http://easymorse-iphone.googlecode.com/svn/trunk/PostXmlDemo1/
Server code: https://mp.myvsp.cn/svn/works/exam/prototype/trunk/iphonepost/