NSString *AuthToken = [[NSUserDefaults standardUserDefaults]
stringForKey:@"AuthToken"];
NSString* json =[NSString stringWithFormat:@"{'DeviceId':'%@','DeviceType':'iOS','UM_Identifier':'%@','AuthToken':'%@','Query':'all'}", deviceId, userEmail, AuthToken];
NSString *post =[[NSString alloc] initWithFormat:@"jinpAllCustDetails=%@",json];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com"]];
NSData *postData = [post dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
在上面的代码中:
从上次网络服务响应收到的身份验证令牌保存在NSUserdefaults中, 然后用于下一个webservice请求。
所以对于Eg。
发送身份验证令牌:z71VxyfVlBxvNKJ01m64a4oKV9lWEv+fFhHxi+7zyRw=
但服务器会将其收到:z71VxyfVlBxvNKJ01m64a4oKV9lWEv fFhHxi 7zyRw=
即所有出现的“+”都被“”替换。因此,服务器将其视为无效的身份验证令牌,并且webservices请求会相应地返回结果。
帮助我解决这个问题,提前谢谢
答案 0 :(得分:0)
这不是有效的JSON,因为字符串应该被"
包围。创建NSDictionary
个值并使用NSJSONSerialization
创建JSON字符串,您知道该字符串有效:
NSDictionary *values = @[
@"DeviceId": deviceId,
@"DeviceType": @"iOS",
@"UM_Identifier": userEmail,
@"AuthToken": authToken,
@"Query": @"all"
];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:values
options:0
error:&error];
NSAssert(jsonData != nil, @"Failed to create JSON data");
NSString jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];