2010-01-07 60 views
4

我要送GET或POST請求爲localhost:香港專業教育學院使用此代碼URL GET/POST請求的Objective-C

<?php 
if(@$_GET['option']) { 
    echo "You said \"{$_GET['option']}\""; 
}else if(@$_POST['option']) { 
    echo "You said \"{$_POST['option']}\""; 
} 
?> 

:在代碼

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php?option=Hello"]]; 
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *get = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

它的工作原理,但一次。如果生病又做了一次,應用程序就終止了。

林嘗試使用ASIFormDataRequest:

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:@"http://localhost/wsh/index.php"] autorelease]; 
[request setPostValue:@"option" forKey:@"myFormField1"]; 
[request start]; 
NSError *error = [request error]; 
if (!error) { 
    NSString *response = [request responseString]; 
    NSLog(response); 
}else{ 
    NSLog(@"error"); 
} 

它說:

2010-01-07 13:20:34.964 WSH[3351:903] -[NSCFString absoluteURL]: unrecognized selector sent to instance 0x160f8 
2010-01-07 13:20:34.966 WSH[3351:903] error 

SRY,我的英語

回答

8

您使用的是普通的NSString文字,其中一個NSURL對象預計: ...] initWithURL:@"http://localhost/wsh/index.php" [...]

將其更改爲initWithURL:[NSURL URLWithString:@"http://localhost/wsh/index.php"]

+0

THANXXX!那工作 – kosmaks 2010-01-07 09:58:05

6

我不知道是否還應該切換值和關鍵崗位值,即改變線

[request setPostValue:@"option" forKey:@"myFormField1"]; 

[request setPostValue:@"myFormField1" forKey:@"option"]; 
+0

是的啊!我知道 )) – kosmaks 2010-01-07 10:23:28