第一次使用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];
在尝试了六种不同的方式之后,我从previous post获得了修改后的版本。
又一次尝试得到了同样的结果。
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];
答案 0 :(得分:2)
有关如何使用POST上传图片的信息,请参阅my answer here。它发布到PHP文件中的表单。发布数据而不上传图像的概念相同,但这是如何使用Objective-C发布到PHP脚本的工作示例。
答案 1 :(得分:0)
尽量不要明确设置content-type
。避免使用以下行:
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
如果您使用的是iPhone模拟器,我强烈建议您使用PROXY进行HTTP调试。 mitmproxy
太棒了。并且非常容易在 Mac OS X 上进行设置。
答案 2 :(得分:0)
使用以下代码。确保帖子字符串的收据是一个密钥,并将在服务器中使用。 客户端代码
NSString *receipt1 = @"username";
NSString *post =[NSString stringWithFormat:@"receipt=%@",receipt1];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://localhost:8888/validateaction.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300)
{
NSLog(@"Response: %@", result);
}
}
服务器端php脚本。
validation.php
<?php
if(_POST)
{
if($_POST['receipt'] == 'username')
{
echo "post successfull";
$receipt = $_POST['key1'];
echo $receipt;
}
else
{
echo "not post";
}
?>