سؤال

Im trying to login to the member area of a website using the obj c, ASIFormDataRequest. I really need some help. Im stuck now. What shall i do to make it work? example code would be great. Thanks.

the login page on the internet:

<form action="https://www.example.org/login.php" method="post" onsubmit="md5hash(vb_login_password, vb_login_md5password, vb_login_md5password_utf, 0)">


<input type="hidden" name="do" value="login" />
<input type="hidden" name="url" value="/login.php" />
<input type="hidden" name="vb_login_md5password" />
<input type="hidden" name="vb_login_md5password_utf" />


<fieldset class="fieldset">
<td>Username:<br /><input type="text" name="vb_login_username"/>
<td>password:<br /><input type="password" name="vb_login_password"/>

<label for="cb_cookieuser"><input type="checkbox" name="cookieuser" value="1" id="cb_cookieuser"/>Remember me?</label>
<input type="submit" class="button" value="Logga in" accesskey="s" tabindex="1" />
<input type="reset" class="button" value="&Aring;terst&auml;ll f&auml;lt" accesskey="r" tabindex="1" />
</table>
</fieldset>


</form>

my code so far:

NSString *username, *password; 
        username = [NSString stringWithFormat:@"%@", usernameField.text];
        password = [NSString stringWithFormat:@"%@", passwordField.text];
        NSLog(@"Trying to sign in with user:%@ pass:%@",username,password);

        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"https://www.example.org/login.php"]];
        [request setPostValue:username forKey:@"vb_login_username"];
        [request setPostValue:password forKey:@"vb_login_password"];
        [request setTimeOutSeconds:120];
        [request setDelegate:self];
        [request startAsynchronous];

+ (NSString*)md5HexDigest:(NSString*)input {
    const char* str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, strlen(str), result);

    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}
هل كانت مفيدة؟

المحلول

Sometime your NSURL does not make properly by using URLWithString, Why have you created two nsstring NSString *username, *password; .If you are not modifying them then you can use them directly like:-

Try this:-

   NSString *str = @"https://www.example.org/login.php";
   NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
   ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  [request setPostValue:usernameField.text forKey:@"vb_login_username"];
  [request setPostValue:passwordField.text forKey:@"vb_login_password"];
  [request setTimeOutSeconds:120];
  [request setDelegate:self];
  [request startAsynchronous];
  NSLog(@"%@",[request responseString]);
  NSLog(@"%d",[request responseStatusCode]);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top