Failed To Send Json With NSUrlconnection

丶灬走出姿态 提交于 2019-12-25 06:48:12

问题


i am implementing this code wich take apple in app purchase receipt from the current transaction (not listed here) i am converting it to base64 NSData object, create new NSString with the values and keys (json object) and send it trough NSUrlconnection . when the compiler hits the init with request the app crashes after 2 seconeds... without to get any response. this is the code.

    NSData *data = [NSData dataFromBase64String:receiptStr];
NSString *jsonString = [NSString stringWithFormat:@"{\"receipt-data\":\"(%@)\",\"password\":\"(%@)\"}", data, SHARED_SECRET];
NSLog(@"%@",jsonString);
savedReceipt = jsonString;
[[NSUserDefaults standardUserDefaults]setValue:savedReceipt forKey:@"savedrecipt"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSData *requestdata = [jsonString dataUsingEncoding:NSUTF8StringEncoding];    //urlData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[NSString stringWithFormat:@"%@",requestdata]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

anyone have an idea what am i doing wrong? i am also new to json so it could be also a problem there.


回答1:


That's because this line in your code:

[request setHTTPBody:[NSString stringWithFormat:@"%@",requestdata]];

Is trying to setHTTPBody to some formatted NSString, when the method is actually expecting NSData.

Just use:

[request setHTTPBody: requestdata];

And see if you have better results.



来源:https://stackoverflow.com/questions/8036267/failed-to-send-json-with-nsurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!