Search code examples
ioshttpnsurl

What is wrong with my NSURL statement?


Please glance at this and tell me why my NSURL *urlStringURL is alway an "invalid CRStringRef" Going blind trying to see what's wrong. Thanks!

EDIT: below code doesn't work since it's a GET. Can't encode the receipt to make a GET work. Good code follows bad code.

- (void) sendReceipt {

NSObject *rData = [[NSUserDefaults standardUserDefaults] objectForKey:@"purchaseReceipt"];

NSNumber *custData = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];

NSString *urlString = [[NSString alloc] initWithString:@"https://www.privusmobile.info/Apple-???/?TYPE=GET"];    

NSLog(@"Transaction to send %@,%@" , rData,custData);


urlString = [urlString stringByAppendingFormat:@"&Cust=IP%@", custData];        
urlString = [urlString stringByAppendingFormat:@"&Receipt=%@", rData];

NSURL *urlStringURL =[NSURL URLWithString:[NSString stringWithFormat:@"%@",urlString]];

NSMutableURLRequest *urlRequest =[NSMutableURLRequest requestWithURL:urlStringURL];
NSString *params = [[NSString alloc]initWithString:urlString];
urlData = [[NSMutableData alloc]init];
[urlRequest setHTTPMethod:@"GET"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];


NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];


NSLog(@"started!");

}

END OF BAD CODE

The good code follows:

    - (void) sendReceipt {

NSObject *rData = [[NSUserDefaults standardUserDefaults] objectForKey:@"purchaseReceipt"];

NSNumber *custData = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://www.privusmobile.info/Apple??????/default.aspx?"]];

[request setHTTPMethod:@"POST"];
[request setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];

NSString *xmlString = [NSString stringWithFormat:@"<data><item>IP%@ </item><item>%@</item></data>",custData,rData];


NSLog(@"XMLSTRING %@",xmlString);

[request setValue:[NSString stringWithFormat:@"%d",[xmlString length]] forHTTPHeaderField:@"Content-length"];

[request setHTTPBody:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

NSLog(@"started! %@",connection);        

}


 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

NSString * strResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"SERVER DATA %@", strResponse);


}

The clue was the encoding hints. Thanks.


Solution

  • Try encoding the url string. Most likely there are spaces in rData but I'm just guessing. Try this:

    NSObject *rData = [[NSUserDefaults standardUserDefaults] objectForKey:@"purchaseReceipt"];
    
    NSNumber *custData = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
    
    NSString *urlString = [[NSString alloc] initWithString:@"https://www.privusmobile.info/Apple-App/?TYPE=GET"];
    
    urlString = [urlString stringByAppendingFormat:@"&Cust=IP%@", custData];        
    urlString = [urlString stringByAppendingFormat:@"&Receipt=%@", rData];
    
    NSString* escapedUrlString =[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    
    NSURL *urlStringURL =[NSURL URLWithString:escapedUrlString];