问题描述:

I couldn't find anything helpful about:

How do I post login-date (username AND password) and login on the webpage?

I know there is AFNetworking, but is there also a solution without it? If not, how do I manage it with AFN ? I can't really get in this AFN-Stuff. (yeah, I know, I suck)

Here's the login Code from the Website:

 <div class="moduletable">

<form action="/" method="post" id="form-login" >

<fieldset class="input">

<p id="form-login-username">

<label for="modlgn_username">Benutzername</label>

<input id="modlgn_username" type="text" name="username" class="inputbox" size="18" />

</p>

<p id="form-login-password">

<label for="modlgn_passwd">Passwort</label><br />

<input id="modlgn_passwd" type="password" name="password" class="inputbox" size="18" />

</p>

<p id="form-login-remember">

<label for="modlgn_remember">Angemeldet bleiben</label>

<input id="modlgn_remember" type="checkbox" name="remember" class="inputbox" value="yes"/>

</p>

<input type="submit" name="Submit" class="button" value="Anmelden" />

<input type="hidden" name="option" value="com_users" />

<input type="hidden" name="task" value="user.login" />

<input type="hidden" name="return" value="aW5kZXgucGhwP0l0ZW1pZD0yODk=" />

<input type="hidden" name="f8abbfe43b9acc4830bb59c5b35cd9ef" value="1" /> </fieldset>

<!--<ul>

<li>

<a href="/component/users/?view=reset">

Passwort vergessen?</a>

</li>

<li>

<a href="/component/users/?view=remind">

Benutzername vergessen?</a>

</li>

</ul>!-->

</form>

</div>

and here is my attempt using a mutableRequest

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.ohg-bensberg.info/component/users/?view=login"]];

[request setHTTPMethod:@"post"];

NSString *string = @"username=blubbblubb&password=blabla";

[request setHTTPBody:[string dataUsingEncoding:NSUTF8StringEncoding]];

NSError *error=nil;

NSURLResponse *response=nil;

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

[webView loadRequest:request];

For real! THANKS A LOT, I definetly need to get this.

网友答案:

First, you should not send synchronous requests. This is completely unacceptable from a user experience perspective. Use it only for testing.

Second, you are sort of duplicating the request by sending it via NSURLConnection and the web view. Either you load the result in the return value of your connection call (it is of type NSData and you are ignoring it); or you just call the web view loadRequest (which is asynchronous).

Third, you should include the hidden data fields. In all likelihood your web server needs those to give a valid response.

相关阅读:
Top