iphone - 将 post 变量发送到 iOS 表单

标签 iphone ios objective-c

我正在尝试将用户名和密码发送到通常通过表单完成的网站。然而,服务器将我重定向到登录页面,这通常在用户名和密码错误时发生。用户名是电子邮件地址的形式,密码是字符串。

我目前可以访问该网站,因为开发人员不在检查值是否得到正确处理。谁能看到我在下面创建的代码中的任何明显错误?

请注意,出于隐私原因,我已从示例代码中删除了 URL。

// Validate login
-(bool)validateLogin{

    // Initialize URL to be fetched
    NSURL *url = [NSURL URLWithString:@"removedurl"];

    NSString *post = @"username=example1%40example.com&password=example2";

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    // Initalize a request from a URL
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];

    //set url
    [request setURL:url];
    //set http method
    [request setHTTPMethod:@"POST"];
    //set request length
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    //set request content type we MUST set this value.
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    //set post data of request
    [request setHTTPBody:postData];

    NSLog(@"%@", [request allHTTPHeaderFields]);

    //initialize a connection from request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    _connection = connection;    
    //start the connection
    [connection start];

    return YES;
}

最佳答案

这个字符串不正确 NSString *post = @"username=example1%40example.com&example2"; 在&符号之后,您必须提供键=值。
@"key1=value1&key2=value2";

工作代码示例:

在 .h 文件中设置委托(delegate):

@interface Controller <NSURLConnectionDataDelegate>

在 .m 文件中:

- (void)login {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kRequestTimeOut];
    request.HTTPMethod = @"POST";
    NSString *params = @"key1=value1&key2=value2";
    request.HTTPBody = [params dataUsingEncoding:NSUTF8StringEncoding];

    _data = [NSMutableData data];

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_data appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    //Parse your '_data' here.
}

关于iphone - 将 post 变量发送到 iOS 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16897918/

相关文章:

ios - 在 MapView 中搜索注释

iphone - 执行 symbolicatecrash 命令时出错 "Can' t exec "/usr/bin/xcode-select": No such file or directory at/usr/bin/symbolicatecrash"

ios - 如何将 UILocalNotification 的 repeatInterval 设置为每两天一次?

ios - 插入 UIViewController 呈现 UINavigationViewController(仅显示一个动画)

iphone - writeToFile 不适用于内部有 UIImagePickerControllerReferenceURL 的 NSDictionary

ios - Swift - 使用 downloadTaskWithURL 下载视频

ios - SpriteKit 物理不工作

ios - AFHTTPRequestOperationManager 是否在 operationQueue 上运行请求?

ios - 界面旋转适用于 iOS 6 但不适用于 iOS5

iPhone,JSON 响应中的特殊字符