如何将url中的数据发送到服务器?

时间:2013-12-02 08:04:02

标签: ios json

http://novatoresols.com/demos/blow/users/add.json?json={"email":"ali"}

如何向网络发送keydata?在这里" 电子邮件"是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];

1 个答案:

答案 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);