如果我想在用户已将用户名和密码放在UITextField中并使用AFNetworking登录的应用程序中登录Instapaper( http://www.instapaper.com/user/login ),我该如何做到这一点?
这个方法有用吗? http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/postPath:parameters:success:failure :
基本上我想获取凭据,登录Instapaper然后与加载的网站进行交互。
如果是上面的方法,我是否使用NSDictionary作为参数,其中'用户名' - >''和'密码' - >''或其他什么? 并且是登录后呈现的页面的HTML响应(我正在寻找什么)?
我是网络新手,所以我有点困惑我如何使用AFNetworking来实现这一目标。
If I wanted to login to Instapaper (http://www.instapaper.com/user/login) within an app where the user has put the username and password within a UITextField and login using AFNetworking, how would I accomplish this?
Is it to do with this method? http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/postPath:parameters:success:failure:
Basically I want to take the credentials, log in to Instapaper and then interact with the website that loads.
If it is the above method, do I use an NSDictionary for parameters where it's 'username'->'', and 'password'->'' or something? And is the response the HTML of the page that presents after login (what I'm looking for)?
I'm new to networking, so I'm a little confused how I'd use AFNetworking to accomplish this.
最满意答案
使用AFHTTPRequestOperation变量AFHTTPClient ,如果您要发送用户名和密码作为参数,然后将它们添加到NSDictionary中,如下所示并将它们作为参数发送,否则您可以将它们作为formData发送,如果您想在体内发送它们。
NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setObject:username.text forKey:@"username"]; [parameters setObject:password.text forKey:@"password"]; AFHTTPClient *afHttpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://yourbaseURL"]]; NSMutableURLRequest *request = [afHttpClient multipartFormRequestWithMethod:@"POST" path:@"/pathifAny" parameters:parameters constructingBodyWithBlock:^(id <AFMultipartFormData>formData) { // here you can send as body if you not setting as parameters [formData appendPartWithFormData:[username.text dataUsingEncoding:NSUTF8StringEncoding] name:@"username"]; [formData appendPartWithFormData:[password.text dataUsingEncoding:NSUTF8StringEncoding] name:@"password"]; }]; AFHTTPRequestOperation *afHttpReqOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];这是关于GET / POST的简单教程
Use AFHTTPRequestOperation variant of AFHTTPClient, If you are sending username and password as parameters then add them into a NSDictionary as follows and send them as params, otherwise you can send them as formData if you want to send them in the body.
NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setObject:username.text forKey:@"username"]; [parameters setObject:password.text forKey:@"password"]; AFHTTPClient *afHttpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://yourbaseURL"]]; NSMutableURLRequest *request = [afHttpClient multipartFormRequestWithMethod:@"POST" path:@"/pathifAny" parameters:parameters constructingBodyWithBlock:^(id <AFMultipartFormData>formData) { // here you can send as body if you not setting as parameters [formData appendPartWithFormData:[username.text dataUsingEncoding:NSUTF8StringEncoding] name:@"username"]; [formData appendPartWithFormData:[password.text dataUsingEncoding:NSUTF8StringEncoding] name:@"password"]; }]; AFHTTPRequestOperation *afHttpReqOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];Here is simple tutorial about GET/POST
更多推荐
发布评论