想要创建字符串到Post方法

时间:2013-04-05 01:55:46

标签: ios objective-c string

我想在POST方法中传递一个字符串..

字符串为@"functionName=getCourses_by_category&json={"course_cat_id":"8"}"

1 个答案:

答案 0 :(得分:0)

来自Apple的how to send POST data文档:

NSString *bodyData = @"functionName=getCourses_by_category&json={\"course_cat_id\":\"8\"}";

NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.apple.com"]];

// Set the request's content type to application/x-www-form-urlencoded
[postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

// Designate the request a POST request and specify its body data
[postRequest setHTTPMethod:@"POST"];
[postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:[bodyData length]]];

// Initialize the NSURLConnection and proceed as usual