2012-07-13 30 views
0

我需要一些後的參數傳遞給服務器,但PHP得到的$ _ POST 空數組 這是我的代碼:Xcode中的WebView未通過POST參數

NSString* user=UserNameField.stringValue; 
NSString* pass=PasswordField.stringValue; 
NSString* ID=IDField.stringValue; 

NSURL *url = [NSURL URLWithString:ENTERANCE]; 
url = [NSURL URLWithString:@"http://sphere-beauty.com/test.php"]; 


NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
NSString* post=[NSString stringWithFormat:@"uname=%@&passwd=%@&id=%@",user,pass,ID]; 

[req setHTTPMethod:@"POST"]; 
[req setHTTPBody: [post dataUsingEncoding:NSUTF8StringEncoding]]; 



[[browser mainFrame] loadRequest:req]; 

誰能幫助?

+0

設置網址然後覆蓋它是什麼意思? – 2012-07-13 16:30:45

+0

那只是爲了測試,看到帖子數據 – Dima 2012-07-13 16:36:40

回答

1
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"www.aaa.com"]]; 
[request setHTTPMethod:@"POST"]; 
NSString *request_body = [NSString stringWithFormat:@"u=aaa&p=aaa"]; 
[request setHTTPBody:[request_body dataUsingEncoding:NSASCIIStringEncoding]]; 
NSError *error; 
NSURLResponse *response; 
NSData *theData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

這是怎麼了,我通常以同步的方式與服務器通信...

+0

順便說一句,你需要使用webview的ur請求? – 2012-07-13 17:30:39

2

此代碼對我的作品,發送一個簡單的POST請求測試頁。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString *[email protected]"Freddy Krueger!"; 
    NSString *encodedName = [name urlencode]; 
    NSString *postString = [NSString stringWithFormat:@"name=%@", encodedName]; 
    NSData *data = [postString dataUsingEncoding:NSASCIIStringEncoding]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.1.100/www/posttest.php"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:data]; 

    [web loadRequest:request]; 

    // Non-ARC 
    // [request release]; 
} 

[name urlencode]呼籲NSString *一個urlencode類中的方法是在這裏找到:How do I URL encode a string

web顯然是我的UIWebView

您的代碼可能失敗,因爲您不是URL編碼參數。