我的NSURL声明有什么问题?

时间:2012-09-18 18:23:04

标签: ios http nsurl

请看一下这个并告诉我为什么我的NSURL * urlStringURL总是一个“无效的CRStringRef”盲目试图看出什么是错的。谢谢!

编辑:下面的代码不起作用,因为它是一个GET。无法对收据进行编码以进行GET工作。好的代码遵循错误的代码。

- (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!");

}

BAD CODE结束

好的代码如下:

    - (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);


}

线索是编码提示。感谢。

1 个答案:

答案 0 :(得分:0)

尝试编码url字符串。最有可能rData中有空格,但我只是在猜测。试试这个:

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];