سؤال

I want to re-execute the NSURLRequest on connection fail event. I want to add a parameter to existing POST parameters.

Code :

NSMutableString * postParams = [[NSMutableString alloc] initWithData:((NSData *)((NSURLRequest *)connection.originalRequest).HTTPBody) encoding:NSUTF8StringEncoding];

// If post parameters "retry" is not found in HTTP body
if ([postParams rangeOfString:@"retry="].location == NSNotFound)
{
    // Append a string with parameter "retry" with value true.
    [postParams stringByAppendingString:[NSString stringWithFormat:@"&retry=%d", true]];
    NSLog(@"Modified params : %@", postParams);
}

After modifying the params in string when I log its value it is unchanged.

How can I add POST param to existing request?

هل كانت مفيدة؟

المحلول

The problem is the method you are using for appending the string.

stringByAppendingString: copies the current string, appends the parameter, and returns the result. It does NOT modify the original string.

The simplest modification to make in your case is probably just to use the appendString: NSMutableString method.

[postParams appendString:[NSString stringWithFormat:@"&retry=%d", true]];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top