Search code examples
iosobjective-ciphonensurlconnection

how to do Http request post using nsurlconnection with multiple header


Here is my code please find correction i can not find why response not receive
NSString *strNumberKey = @"1234567890"; NSString *strTimeStamp = [[AppDelegate appDelegate]returnTimestamp];//generate random number

    NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"your_Method_Name",@"MethodName",strTimeStamp,@"RequestUniqueID",[AppDelegate appDelegate].strSessionID,@"SessionID",nil];


    NSString *jsonString = [jsonDictionary JSONRepresentation];
    NSLog(@"Json Format----------%@",jsonString);


    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat: COMMON_URL_DISTRIBUTOR]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
    [req setHTTPMethod:@"POST"];
    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req setValue:strNumberKey forHTTPHeaderField:@"Number"];


    NSString *strEncrypt = [FBEncryptorAES encryptBase64String:jsonString keyString:strEncryption];

    [req setValue:[strEncrypt stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]  forHTTPHeaderField :@"Data"];


    NSString* theBodyString = [NSString stringWithFormat:@"Data=%@",strEncrypt];
    NSString* escapedUrlString = [theBodyString stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    NSData *requestData = [escapedUrlString dataUsingEncoding:NSUTF8StringEncoding];

    [req setHTTPBody:requestData];
    (void)[[NSURLConnection alloc] initWithRequest:req delegate:self];

    responseData = [NSMutableData data];

Solution

  • Make correction It would be run correctly hope so,

    NSString* theBodyString = [NSString stringWithFormat:@"Number=%@&Data=%@",strNumberKey,strEncrypt];