来自NSMutablearray的setheader值

时间:2018-02-03 06:40:57

标签: ios objective-c post http-post

我面临着从NSMutableArray获取价值的问题。 我想动态地解析requestHeader的值。 我是服务器上的POST数据。

喜欢:

[theRequest setValue:@"application/json"  forHTTPHeaderField:@"Content-Type"];

如果我单独使用每个部分,这将有效。 但是如果我想用 NSMutableArray 尝试相同的值,这就是我的异常。

CODE

for (int j = 0; j < arraySize; j=j+2)
    {
        NSLog (@"Element  = %@",  [randomSelection objectAtIndex: j]);
        NSLog (@"Element  = %@",  [randomSelection objectAtIndex: j+1]);
        NSString * setVal = [randomSelection objectAtIndex: j];
        NSString * setHead = [randomSelection objectAtIndex: j+1];
         [theRequest setValue:setVal  forHTTPHeaderField:setHead];

    } 

以下是我文件的完整代码。

//username & password  allow
   NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
     NSString *jsonStrq = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"jsonStrq: %@",jsonStrq);


    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:requestUrl      cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
 [theRequest setHTTPMethod:@"POST"];

  //long count = [randomSelection count];
    //here is the issue
    for (int j = 0; j < arraySize; j=j+2)
    {
        NSLog (@"Element  = %@",  [randomSelection objectAtIndex: j]);
        NSLog (@"Element  = %@",  [randomSelection objectAtIndex: j+1]);
        NSString * setVal = [randomSelection objectAtIndex: j];
        NSString * setHead = [randomSelection objectAtIndex: j+1];
         [theRequest setValue:setVal  forHTTPHeaderField:setHead];

    }


 //commented code [theRequest setValue:@"application/json"  forHTTPHeaderField:@"Content-Type"];

    [theRequest setHTTPBody:[jsonStrq dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLResponse *theResponse = NULL;
    NSError *theError = NULL;
    [NSURLConnection sendAsynchronousRequest:theRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){

        if (error)
        {
            printf("ios Error send value %s", [error localizedDescription]);
        }
        else
        {
            sendHttpData(cHttpClient,[data length],[data bytes],200);
            printf("data length %d",[data length]);
        }

    }];


    NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];



    NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:0 error:&theError];
    NSLog(@"url to send request= %@",theResponseData);
    NSLog(@"%@",dataDictionaryResponse);

记录错误:

018-02-07 10:10:36.148563+0530 adwitiyaios[297:37666] Task <B384CA36-FFE9-43F5-851A-09926637E202>.<0> HTTP load failed (error code: -1005 [4:-4])
2018-02-07 10:10:36.152646+0530 adwitiyaios[297:37797] NSURLConnection finished with error - code -1005
ios Error send value hЪ\2622018-02-07 10:10:36.156287+0530 adwitiyaios[297:37730] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(0x1818f2364 0x180b38528 0x1818f22ac 0x182255f34 0x10005519c 0x100157074 0x100156be0 0x100153aa8 0x10018b258 0x1001ddc60 0x10018b3e8 0x10018bb2c 0x1001f7c44 0x100208ea0 0x1815182b4 0x181518180 0x181516b74)
libc++abi.dylib: terminating with uncaught exception of type NSException
2018-02-07 10:11:04.988149+0530 adwitiyaios[297:37990] XPC connection interrupted

1 个答案:

答案 0 :(得分:1)

NSURLRequest标题为NSDictionary,因此您可以填写自定义标题字典,并使用方法setAllHTTPHeaderFields将自定义标题设置为您的请求

尝试使用此

NSMutableDictionary * customHeaders = [NSMutableDictionary dictionary];
for (int j = 0; j < arraySize; j=j+2)
{
    NSLog (@"Element  = %@",  [randomSelection objectAtIndex: j]);
    NSLog (@"Element  = %@",  [randomSelection objectAtIndex: j+1]);
    NSString * setVal = [randomSelection objectAtIndex: j];
    NSString * setHead = [randomSelection objectAtIndex: j+1];
    [customHeaders setObject:setVal forKey:setHead]
}

[theRequest setAllHTTPHeaderFields:customHeaders];