我要發送一個簡單的消息(的NSString)到我的PHP腳本。我正在嘗試執行POST請求。我無法做到這一點,我不知道我做錯了什麼。有人能告訴我我的錯誤嗎?簡單的POST消息服務器
謝謝!
PHP代碼
<?php
if($_POST['error'])
{
$date= time();
$error=$_POST['error'];
$message=$date." ".$error;
echo $message;
//Rest of code just writes to a log file
?>
---------------------
NSString *post =[[NSString alloc] initWithFormat:@"error=%@",serverError];
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://mysite.com/error.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData is %@", responseData);
請注意,這個問題不是關於Xcode。 – 2013-05-28 06:40:59
迴應是什麼?或者根本沒有迴應?嘗試打印錯誤描述,就像你打印了響應數據一樣......你可能會有一些想法..如果你不這樣做,請在你的問題中發佈回覆/錯誤... – lukya
@lukya所以反應是不是寫入文件和NSLog返回「ResponseData is <>」謝謝! – Teddy13