我创建了一个字典“email”和“password”的字典,并使用这些参数作为我的请求的主体。我得到了我的服务器的响应,我错过了参数电子邮件和密码 - 我很肯定这不是服务器端的问题。这就是我正在形成请求的方式:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *urlString = @"xxxxxx.xxxx.com/userServices/login";
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSDictionary *params = @{@"email" : email, @"password" : password};
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
[request setHTTPBody:bodyData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:45];
[request setHTTPShouldHandleCookies:NO];
知道为什么服务器不会识别我的参数吗?
答案 0 :(得分:1)
创建字典时,我会这样做
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setValue:[NSString stringWithFormat:@"%@", email] forKey:@"email"];
[params setValue:[NSString stringWithFormat:@"%@", password] forKey:@"password"];
之后我会这样做
NSData *postData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
您可能需要为您的请求提供内容长度
[request setValue:[NSString stringWithFormat:@"%i",postData.length] forHTTPHeaderField:@"Content-Length"];
答案 1 :(得分:0)
NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",[txtUserEmail text],[txtUserPassword text]];
// what ever in you case e.g [txtuseremail text] replaced by email
NSURL *url=[NSURL URLWithString:@"http://localhost/abc.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
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" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
尝试这可能会对你有所帮助
答案 2 :(得分:0)
试试这个会对你有用:
NSDictionary * requestDict = [NSDictionary dictionaryWithObjectsAndKeys:@“email”,@“YourEmail”,@“password”,@“YourPassword”,nil];
NSMutableURLRequest * request =[[ NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:BASE_URL]];
NSData * postData =[ NSJSONSerialization dataWithJSONObject:requestDict options:0 error:nil];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLConnection
sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
if ([data length] >0 && error == nil)
{
NSDictionary * dictServerResponse =[ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"data %@",dictServerResponse);
// DO YOUR WORK HERE
}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}
}];