Iphone 在iOS中发布请求?
我正在尝试在iOS应用程序中执行POST请求 下面的行在我的Mac的命令行中按预期工作Iphone 在iOS中发布请求?,iphone,ios,networking,post,curl,Iphone,Ios,Networking,Post,Curl,我正在尝试在iOS应用程序中执行POST请求 下面的行在我的Mac的命令行中按预期工作 curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"email":"frank.smith@gmail.com","password":"frankSmith"}' http://server.address 然而,我似乎无法将其正确转换为iOS。以下是我尝试过的: NSURL *url =
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"email":"frank.smith@gmail.com","password":"frankSmith"}' http://server.address
然而,我似乎无法将其正确转换为iOS。以下是我尝试过的:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",SERVER_ADDRESS]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *messageBody = [NSString stringWithFormat:@"{\"email\":\"%@\",\"password\":\"%@\"}",@"frank.smith@gmail.com",@"frankSmith"];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
我错过了什么/做错了什么?找到了答案:
我只需要添加一行:
[请求设置HttpShouldHandleCookies:否]代码>算出了:
我只需要添加一行:
[请求设置HttpShouldHandleCookies:否]代码>