第一次使用 POST
POST 信息未从 iPhone 获取到 PhP 脚本。我在任何地方都没有错误。var_dump($_POST) 显示为空。登录失败返回。任何想法下一步看哪里或者我在这里有一个明显的问题:
NSURL *url = [NSURL URLWithString:link];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *requestBodyString = [NSString stringWithFormat:@"txtuid=%@&txtpwd=%@", userName , password];
NSData *requestBody = [NSData dataWithBytes:[requestBodyString UTF8String]length:[requestBodyString length]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestBody];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; // added from first suggestion
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
在尝试了六种不同的方式后,我从之前的帖子中获取了修改后的版本。
又一次尝试,结果相同。
NSData *data = [[NSString stringWithFormat: @"txtuid=%@&txtpwd=%@", userName , password] dataUsingEncoding:NSUTF8StringEncoding];
// set up request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:link]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"--------------------------------------------------"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// body
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];