2012-07-13 72 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编码参数。