POST提交数据并获取返回值(XML格式的字符串)

@try {
        NSError *error;
        NSURL *url = [NSURL URLWithString:@"http://www.corp.com/info.xml"];

        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        
        [dic setValue:@"14" forKey:@"type"];
        [dic setValue:@"POST" forKey:@"method"];
        [dic setValue:@"1" forKey:@"siteid"];
         
        NSString *myBounds = [[NSString alloc] initWithString:@"test"];
        NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:url];
        NSString *myContent = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",myBounds];
        [myRequest setValue:myContent forHTTPHeaderField:@"content-type"];
        [myRequest setHTTPMethod:@"POST"];
        [myRequest setHTTPBody:[self createFormData:dic withBoundary:myBounds]];
        
        NSURLResponse *response;
        NSData *myReturn = [NSURLConnection sendSynchronousRequest:myRequest returningResponse:&response error:&error];
        
        NSString *str = [[NSString alloc] initWithData:myReturn encoding:NSUTF8StringEncoding];
    }
    @catch (NSException *exception) {
        //
    }
    @finally {
        //
    }


- (NSData*)createFormData:(NSDictionary *)myDictionary withBoundary:(NSString *)myBounds{
    NSMutableData *myReturn = [[NSMutableData alloc] initWithCapacity:10];
    NSArray *formKeys = [myDictionary allKeys];
    
    for(int i=0;i<[formKeys count];i++){
        [myReturn appendData:[[NSString stringWithFormat:@"--%@\n",myBounds] dataUsingEncoding:NSUTF8StringEncoding]];
        [myReturn appendData:[[NSString stringWithFormat:@"content-disposition: form-data; name=\"%@\"\n\n%@\n", [formKeys objectAtIndex:i], [myDictionary valueForKey:[formKeys objectAtIndex:i]]] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    [myReturn appendData:[[NSString stringWithFormat:@"--%@--\n", myBounds] dataUsingEncoding:NSUTF8StringEncoding]];
    return myReturn;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值