Nsurlrequest Post Mode Request Server sample

Source: Internet
Author: User

http://lizhuang.iteye.com/blog/1833297

1. Preparation Stage

NSString *urlstring = [NSString stringwithformat:@ "http://jssb.zust.edu.cn/androidLogin.action"];

Nsmutableurlrequest *request = [[[[Nsmutableurlrequest alloc] init] autorelease];

[Request Seturl:[nsurl urlwithstring:urlstring];

[Request sethttpmethod:@ "POST"];



2, set the head

NSString *contenttype = [NSString stringwithformat:@ "Text/xml"];

[Request Addvalue:contenttype Forhttpheaderfield: @ "Content-type"];





3, the data content body settings

Nsmutabledata *postbody = [Nsmutabledata data];

[Postbody appenddata:[[nsstring stringwithformat:@ "id=%@&password=%@&role=%@", @ "admin02", @ "admin02", @ " Dean "] datausingencoding:nsutf8stringencoding];

[Request Sethttpbody:postbody];



When the XML is delivered:

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];



4. Request 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];

Nsurlrequest Post Mode Request Server sample

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.