什么是NSMutableURLRequest中的httpBody?

时间:2015-02-25 09:07:30

标签: ios objective-c request nsurlconnection

我需要手动创建请求,而不是使用AFNetworking。

NSString *baseURL = @"http://pl-dev2.railwaymen.org:4213/";
NSString *URLString = [NSString stringWithFormat:@"events/%@", identifier];

NSURL *url = [NSURL URLWithString:URLString relativeToURL:baseURL];
//final URL: http://pl-dev2.railwaymen.org:4213/events/6572673456

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"PUT"];

现在我想发送一些参数:

NSDictionary *params = @{
    "a": "valueA", 
    "b": 23, 
    "c": [NSNull null], 
    "d": ["lisbona", "warsaw", "ny"], 
    "e": UIImageJPEGRepresentation(image, 1)
};
  1. 我该怎么做?

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:nil error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    

    不幸的是无效

  2. 如何查看最终请求网址?

    http://pl-dev2.railwaymen.org:4213/events/6572673456?a=valueA&b=23&c=&d[]=lisbona&d[]=warsaw&d[]=ny&e=
    
  3. 该查询的哪一部分是httpBody?每次?之后。

  4. 参数ce怎么样?

  5. 工作原理?

0 个答案:

没有答案