ASIFormDataRequest的帖子类型

时间:2013-03-06 13:50:39

标签: web-services ios5 asiformdatarequest

我需要发送带有帖子类型的RestFul Web服务请求,后期数据格式如下所示{“Request”:“parameters”....}和具有相同请求的二进制数据。如果可能,可以使用ios请解释一下?

1 个答案:

答案 0 :(得分:0)

我不建议再使用ASIHttpRequest库,因为它没有从开发者那里得到任何更新,关于如何创建帖子http请求这里是一个例子

NSString *url = @"your webservice base url";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];

[request setHTTPMethod:@"POST"];

NSString *param2 = ...//
NSData *binaryData = .... //initilize the binary data you want to send
NSString *bodyString = [NSString stringWithFormat:@"param1=%@&param2=%@",binartData,param2];



[request setValue:[NSString stringWithFormat:@"%d", [bodyString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[bodyString dataUsingEncoding:NSASCIIStringEncoding]];//or set the type of encoding agreed with your webservice

NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *responseString;
if ( responseData && !error){
    responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

}