将发布变量发送到iOS表单

时间:2022-11-16 09:15:00

I am trying to send a username and password to a website which is normally done through a form. The server however is redirecting me to a login page, which usually occurs when the username and password is wrong. The username is in the form of an email address and the password is a string.

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

I do currently have access to the website as the developer is away to check that the values are being processed correctly. Can anyone see any obvious errors in the code I have created below?

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

Please note I have removed the URL from the example code for privacy reasons.

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

// 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;
}

1 个解决方案

#1


3  

This string is not correct NSString *post = @"username=example1%40example.com&example2"; After ampersand you have to provide key=value.
@"key1=value1&key2=value2";

此字符串不正确NSString * post = @“username = example1%40example.com&example2”;在&符之后你必须提供key = value。 @ “键1 =值&键2 =值”;

Example of working code:

工作代码示例:

In .h file set delegate:

在.h文件集委托中:

@interface Controller <NSURLConnectionDataDelegate>

In .m file:

在.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.
}

#1


3  

This string is not correct NSString *post = @"username=example1%40example.com&example2"; After ampersand you have to provide key=value.
@"key1=value1&key2=value2";

此字符串不正确NSString * post = @“username = example1%40example.com&example2”;在&符之后你必须提供key = value。 @ “键1 =值&键2 =值”;

Example of working code:

工作代码示例:

In .h file set delegate:

在.h文件集委托中:

@interface Controller <NSURLConnectionDataDelegate>

In .m file:

在.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.
}