我有一个PHP脚本的服务器发送POST到一个PHP脚本的服务器
<?php
// retrieve POST vars
$comment = $_POST['comment'];
$email = $_POST['email'];
// remove trailing whitespace etc
$comment = trim($comment);
$email = trim($email);
// format date accoring to http://www.w3schools.com/php/func_date_date.asp
$date_time = date("Y-m-d-H-i-s"); // i.e. 2010-04-15-09-45-23
// prepare message
$to = "(removed my email addy)"; //who to send the mail to
$subject = "Feedback from iPhone app"; //what to put in the subject line
$message = "Name/Email: $email \r\n Comment: $comment";
mail($to, $subject,$message) or "bad";
echo "ok";
?>
上我现在该如何从我的iPhone应用程序发送POST请求在...
香港专业教育学院试过这种的东西...
NSURL *url = [NSURL URLWithString:@"mysite.com/script.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *message = boxForSuggestion.text;
NSString *userEmailString = usersEmail.text;
NSString *requestBodyString = [NSString stringWithFormat:@"comment:%@&email%@", message , userEmailString];
NSData *requestBody = [requestBodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"
[request setHTTPBody:requestBody];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; // added from first suggestion
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@", responseString);
任何想法?谢谢你们
山姆
编辑:
我觉得有什么不对我的请求主体
NSData *requestBody = [requestBodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"
[request setHTTPBody:requestBody];
记得我的PHP变量$评论和$电子邮件
我不知道Objective C,也没有任何iPhone API,但是您应该设置正确的MIME类型(application/x-www-form-urlencoded)。 POST数据应该基本上是URL编码的:'key = value&etc' – Thorarin 2010-04-14 10:30:07
我该怎么做? Thorarin可能不知道...其他人? – 2010-04-14 10:39:04