ASI自定义post请求

 参数是字典的post请求

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":@"bubiqudong",

                              @"Password":@"123456",

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

                              @"Age":@"10",

                              @"Sex":@"",

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

                              @"ClientType":clientType,

                              @"DeviceIdentifier":udid,

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

        //字典转成data

        NSData *jsonData = [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",jsonData.length]];

        //请求体

        [request setPostBody:[NSMutableData dataWithData:jsonData]];

        //开始请求

        [request startAsynchronous];


 参数是标准的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 = [GDataXMLElement elementWithName:@"IP" stringValue:@"192.168.11.32"];

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

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

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

        [positionEle addChild:IPEle];

        [positionEle addChild:LongitudeEle];

        [positionEle addChild:latitudeELe];

        

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

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

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

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

        [rootEle addChild:positionEle];

        [rootEle addChild:passwordEle];

        [rootEle addChild:nameEle];

        [rootEle addChild:statusEle];

        //xmlString -> xmlData

        NSData *xmlData = [rootEle.XMLString dataUsingEncoding:NSUTF8StringEncoding];

        //创建请求

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

        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL: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 = [GDataXMLElement elementWithName:@"Token" stringValue:self.token];

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

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

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

        [rootEle addChild:tokenEle];

        [rootEle addChild:headimageEle];

        [rootEle addChild:imageTypeEle];

        

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

        [download startPostWithJsonOrXmlString:rootEle.XMLString];



 参数是soap的post请求


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

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: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 = [NSString stringWithFormat:@"%@%@%@",@"<?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 = [str dataUsingEncoding:NSUTF8StringEncoding];

    

    //请求头

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

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

    //请求体

    [request setHTTPBody:xmlData];

    

    [NSURLConnection connectionWithRequest:request delegate:self];







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值