POST 自定义请求

来源:互联网 发布:微信摇一摇比赛软件 编辑:程序博客网 时间:2024/06/14 19:00

1.ASI请求

NSURL* url = [NSURL URLWithString:@"http://192.168.88.8/app/qianfeng/ichat/register.php"];
        //客户端类型 iphone ipad        NSString* clientType = [[UIDevice currentDevice] model];        //设备标示        NSString* udid = [[UIDevice currentDevice] uniqueDeviceIdentifier];        //参数字典        NSDictionary* dic = @{                @"Name":@"1417",                @"Password":@"1417",                @"Email":@"1417@163.com",                @"Age":@"90",                @"Sex":@"男",                @"Description":@"千锋",                @"ClientType":clientType,                @"DeviceIdentifier":udid,                @"Address":@"永泰庄",                };
<span style="white-space:pre"></span>//dic->data
        NSData* data = [NSJSONSerialization dataWithJSONObject:dic options:0 error:nil];                //asi请求        ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];        request.delegate = self;        request.tag = 1;        //请求类型        [request setRequestMethod:@"POST"];        //请求头        [request addRequestHeader:@"Content-Type" value:@"Application/x-www-form-data"];        [request addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d", data.length]];        //请求体        [request setPostBody:[NSMutableData dataWithData:data]];        //开始请求        [request startAsynchronous];

2.

/*

         <root>           <Position>             <IP>192.168.11.32</IP>             <Longitude>45.222122</Longitude>              <Latitude>116.22222</Latitude>           </Position>           <Password>123456</Password>            <Name>oyangjian</Name>            <Status>hidden</Status>         </root>         */        GDataXMLElement* ipEle = [GDataXMLElement elementWithName:@"IP" stringValue:@"192.168.11.32"];        GDataXMLElement* longitude = [GDataXMLElement elementWithName:@"Longitude" stringValue:@"45.222122"];        GDataXMLElement* latitude = [GDataXMLElement elementWithName:@"Latitude" stringValue:@"116.22222"];        GDataXMLElement* position = [GDataXMLElement elementWithName:@"Position"];        [position addChild:ipEle];        [position addChild:longitude];        [position addChild:latitude];        GDataXMLElement* password = [GDataXMLElement elementWithName:@"Password" stringValue:@"1417"];        GDataXMLElement* name = [GDataXMLElement elementWithName:@"Name" stringValue:@"1417"];        GDataXMLElement* status = [GDataXMLElement elementWithName:@"Status" stringValue:@"hidden"];        GDataXMLElement* root = [GDataXMLElement elementWithName:@"root"];        [root addChild:position];        [root addChild:password];        [root addChild:name];        [root addChild:status];        //xmlString->xmlData        NSData* xmlData = [root.XMLString dataUsingEncoding:NSUTF8StringEncoding];        //创建请求        NSURL* url = [NSURL URLWithString:@"http://192.168.88.8/app/qianfeng/ichat/login.php"];        ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];        request.tag = 2;        request.delegate = self;        //请求方式        [request setRequestMethod:@"POST"];        //请求头        [request addRequestHeader:@"Content-Type" value:@"Application/x-www-form-data"];        [request addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d", xmlData.length]];        //请求体        [request setPostBody:[NSMutableData dataWithData:xmlData]];        [request startAsynchronous];
3.
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; "><pre name="code" class="objc">UIImage* image = [UIImage imageNamed:@"01.png"];        NSData* imageData = UIImagePNGRepresentation(image);        //base64编码        NSString* imageStr = [GTMBase64 stringByEncodingData:imageData];        /*         <root>          <Token></Token>          <HeadImage></HeadImage>          <ImageType>image/png</ImageType>         </root>         */        //创建xml        GDataXMLElement* Token = [GDataXMLElement elementWithName:@"Token" stringValue:self.token];        GDataXMLElement* headImage = [GDataXMLElement elementWithName:@"HeadImage" stringValue:imageStr];        GDataXMLElement* imageType = [GDataXMLElement elementWithName:@"ImageType" stringValue:@"image/png"];        GDataXMLElement* root = [GDataXMLElement elementWithName:@"root"];        [root addChild:Token];        [root addChild:headImage];        [root addChild:imageType];                PKDownload* download = [[PKDownload alloc] initWithURL:@"http://192.168.88.8/app/qianfeng/ichat/upload_headimg.php" Delegate:self];        [download startPostWithJsonOrXmlString:root.XMLString];







0 0
原创粉丝点击