Xcode WebView没有传递POST参数

时间:2012-07-13 16:28:17

标签: xcode macos webview httprequest

我需要将一些帖子参数传递给服务器但是php得到一个空数组 $ _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];

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

此代码适用于我,向测试页发送简单的POST请求。

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *name=@"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]在此处找到的urlencode上调用NSString *类别方法:How do I URL encode a string

web显然是我的UIWebView

您的代码可能失败,因为您不是对参数进行URL编码。

答案 1 :(得分: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];

这就是我通常以同步方式与服务器通信的方式......

相关问题