我正在尝试使用POST请求URL解析参数,但是mypage.php没有收到此参数...... 这是我的代码:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://myurl/mypage.php"]];
NSString *params = [[NSString alloc]initWithFormat:@"name=%@&surname=%@&location=%@&email=%@&password=%@&gender=%@&tipo=%@", name, surname, location, email, password, gender, tipo];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(params);
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
else
{
}
和mypage.php
if($_POST['name'] != "" && $_POST['email'] != "")
//Here the $_POST['name'] and the $_POST['email'] are empty...
答案 0 :(得分:0)
您没有开始连接。你应该用[conn start];
答案 1 :(得分:0)
试试这个:
NSString *params=[NSString stringWithFormat:@"name=%@&surname=%@&location=%@&email=%@&password=%@&gender=%@&tipo=%@", name, surname, location, email, password, gender, tipo];
NSData *postData=[params dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://myurl/mypage.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[request setValue:[NSString stringWithFormat:@"%i",postData.length] forHTTPHeaderField:@"Content-Length"];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (data!=nil) {
NSString *output=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"output: %@", output);
}else{
NSLog(@"data is empty");
}