0

我需要发送带有 post 类型的 RestFul Web Service Request,post 数据格式如下 {"Request":"parameters"....

4

1 回答 1

0

我不再推荐使用 ASIHttpRequest 库,因为它没有从开发人员那里得到任何更新,关于如何创建一个 post 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];

}
于 2013-03-06T14:07:17.053 回答