ASI自定义post请求

来源:互联网 发布:手机定时提醒软件 编辑:程序博客网 时间:2024/05/21 08:01

 参数是字典的post请求

NSURL *url = [NSURLURLWithString:@"http://192.168.88.8/app/qianfeng/ichat/register.php”];

       //客户端类型  iphone  ipad

       NSString *clientType = [[UIDevicecurrentDevice] model];

       //设备标示

       NSString *udid = [[UIDevicecurrentDevice] uniqueDeviceIdentifier];

        

       //参数字典

        NSDictionary *dic =@{@"Name":@"bubiqudong",

                             @"Password":@"123456",

                             @"Email":@"xiao@qq.com",

                             @"Age":@"10",

                             @"Sex":@"",

                             @"Description":@"带着灵魂",

                             @"ClientType":clientType,

                             @"DeviceIdentifier":udid,

                             @"Address":@"旅行"};

        //字典转成data

       NSData *jsonData = [NSJSONSerializationdataWithJSONObject:dic options:0 error:nil];

       //asi

       ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];

        request.delegate =self;

        request.tag =1;

       //请求类型

        [requestsetRequestMethod:@"POST"];

       //请求头

        [request addRequestHeader:@"Content-Type"value:@"Application/x-www-form-data"];

        [requestaddRequestHeader:@"Content-Length"value:[NSStringstringWithFormat:@"%d",jsonData.length]];

       //请求体

        [requestsetPostBody:[NSMutableDatadataWithData:jsonData]];

       //开始请求

        [requeststartAsynchronous];


 参数是标准的XML的post请求


  /*

         <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 = [GDataXMLElementelementWithName:@"IP"stringValue:@"192.168.11.32"];

        GDataXMLElement *LongitudeEle = [GDataXMLElementelementWithName:@"Longitude"stringValue:@"45.222122"];

        GDataXMLElement *latitudeELe = [GDataXMLElementelementWithName:@"Latitude"stringValue:@"116.22222"];

        GDataXMLElement *positionEle = [GDataXMLElementelementWithName:@"Position"];

        [positionEleaddChild:IPEle];

        [positionEleaddChild:LongitudeEle];

        [positionEleaddChild:latitudeELe];

        

        GDataXMLElement *passwordEle = [GDataXMLElementelementWithName:@"Password"stringValue:@"123456"];

        GDataXMLElement *nameEle = [GDataXMLElementelementWithName:@"Name"stringValue:@"bubiqudong"];

        GDataXMLElement *statusEle = [GDataXMLElementelementWithName:@"Status"stringValue:@"hidden"];

        GDataXMLElement *rootEle = [GDataXMLElementelementWithName:@"root"];

        [rootEle addChild:positionEle];

        [rootEle addChild:passwordEle];

        [rootEle addChild:nameEle];

        [rootEle addChild:statusEle];

        //xmlString -> xmlData

        NSData *xmlData = [rootEle.XMLStringdataUsingEncoding:NSUTF8StringEncoding];

        //创建请求

        NSURL *url = [NSURLURLWithString:@"http://192.168.88.8/app/qianfeng/ichat/login.php"];

        ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];

        request.delegate =self;

        request.tag =2;

        //请求方式

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


 参数是自定义的XML的post请求

     //   NSURL *url = [NSURL URLWithString:@"http://192.168.88.8/app/qianfeng/ichat/upload_headimg.php"];

        UIImage *img = [UIImage imageNamed:@"2_10.jgp"];

        NSData *imgData = UIImageJPEGRepresentation(img,1);

        //base64编码

        NSString *imgStr = [GTMBase64 stringByEncodingData:imgData];

        /*

         <root>

         <Token>NTM0Km95YW5namlhbjIyKjEyMzQ1NioxMzcyNTk2MTEx</Token> <HeadImage>iVBORw0KGgoAAAANSUhEUg

         ...AAAiYAAAGcCAIAAABiFfyfAAAAHGlET1QA </HeadImage>!

         <ImageType>image/png</ImageType> 

         </root>

         */

        GDataXMLElement *tokenEle = [GDataXMLElementelementWithName:@"Token"stringValue:self.token];

        GDataXMLElement *headimageEle = [GDataXMLElementelementWithName:@"HeadImage"stringValue:imgStr];

        GDataXMLElement *imageTypeEle = [GDataXMLElementelementWithName:@"ImageType"stringValue:@"image/jpg"];

        GDataXMLElement *rootEle = [GDataXMLElementelementWithName:@"root"];

        [rootEle addChild:tokenEle];

        [rootEle addChild:headimageEle];

        [rootEle addChild:imageTypeEle];

        

        PKDownload *download = [[PKDownloadalloc] initWithURL:@"http://192.168.88.8/app/qianfeng/ichat/upload_headimg.php"Delegate:self];

        [download startPostWithJsonOrXmlString:rootEle.XMLString];



 参数是soap的post请求


    NSURL *url = [NSURLURLWithString:@"http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx"];

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];

    //设置请求方式

    [request setHTTPMethod:@"POST"];

    

//    @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"

//    @"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"

//    @" <soap12:Body>"

//    @" <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"

//    @" <theCityName>"

//    @"_textField.text"

//    @" </theCityName>"

//    @" </getWeatherbyCityName>"

//    @" </soap12:Body>"

//    @" </soap12:Envelope>";


    

    

    NSString *str = [NSStringstringWithFormat:@"%@%@%@",@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"

                     @"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"

                    @" <soap12:Body>"

                     @" <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"

                     @" <theCityName>",_textField.text,@" </theCityName>"

                     @" </getWeatherbyCityName>"

                    @" </soap12:Body>"

                    @" </soap12:Envelope>"];

    NSData *xmlData = [strdataUsingEncoding:NSUTF8StringEncoding];

    

    //请求头

    [request setValue:@"application/soap+xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];

    [request setValue:[NSStringstringWithFormat:@"%d",xmlData.length]forHTTPHeaderField:@"Content-Length"];

    //请求体

    [request setHTTPBody:xmlData];

    

    [NSURLConnectionconnectionWithRequest:request delegate:self];







0 0