http://novatoresols.com/demos/blow/users/add.json?json={"email":"ali"}
如何向网络发送key
和data
?在这里" 电子邮件"是key
和" 阿里"是URL中的值。
代码:
NSURL *url=[NSURL URLWithString:[hostURL stringByAppendingFormat:@"users/add.json?json="]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString * params =@"{\"email\":\"Ali\"}";
[request setHTTPMethod:@"POST"];
// This is how we set header fields
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
答案 0 :(得分:1)
试试这个:
NSString * params =@"{\"email\":\"Ali\"}";
NSURL *url=[NSURL URLWithString:[hostURL stringByAppendingFormat:@"users/add.json?json=%@",params]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog (@"%@",data);