我正在创建我的第一个iPad应用程序.我有一个Web应用程序,我想要以RESTful方式进行身份验证并从中提取数据.
如果您在浏览器中打开URL(https://myapp.com/auth/login),您将获得一个表单来输入您的用户名和密码.我以为我可以在请求的发布数据中设置登录凭据并提交数据.
该站点使用HTTPS进行登录,因此凭据不会通过Internet以纯文本形式传递.
如何建立安全的HTTPS连接以传递凭据?请记住我是否已登录以备将来使用?做这个的最好方式是什么?
我正在使用AFNetworking并创建一个我需要json反馈的帖子请求.下面的代码有效,但我有两个主要问题; 我在哪里发布ActivityIndicator Manager?第二个问题是这个代码是正确的,新的我与块混淆所以我真的想知道我是否正在做正确的事情以获得最佳性能,即使它有效.
NSURL *url = [NSURL URLWithString:@"mysite/user/signup"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
AFNetworkActivityIndicatorManager * newactivity = [[AFNetworkActivityIndicatorManager alloc] init];
newactivity.enabled = YES;
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
usernamestring, @"login[username]",
emailstring, @"login[email]",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"mysite/user/signup"parameters:params];
[httpClient release];
AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id json) {
NSString *status = [json valueForKey:@"status"];
if ([status isEqualToString:@"success"]) {
[username resignFirstResponder];
[email resignFirstResponder];
[self.navigationController dismissModalViewControllerAnimated:NO];
}
else {
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Login Unsuccessful"
message:@"Please try again"
delegate:NULL
cancelButtonTitle:@"OK"
otherButtonTitles:NULL]; …Run Code Online (Sandbox Code Playgroud) 简短的背景故事,我们以前的开发人员使用ASIHTTPRequest发出POST请求并从我们的Web服务中检索数据.由于未知原因,我们的应用程序部分停止工作.似乎有足够的时间来进行未来的证明并与AFNetworking一起使用.REST webservice在CakePHP框架上运行.
简而言之,我没有使用AFNetworking接收请求响应字符串.
我知道web服务有效,因为我能够成功发布数据并使用curl接收正确的响应:curl -d"data [Model] [field0] = field0value&data [Model] [field1] = field1value" https://example.com /api/class/function.plist
根据之前开发人员的说明,我提出了以下建议.
#import "AFHTTPRequestOperation.h"
…
- (IBAction)loginButtonPressed {
NSURL *url = [NSURL URLWithString:@"https://example.com/api/class/function.plist"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[usernameTextField text] forHTTPHeaderField:@"data[User][email]"];
[request setValue:[passwordTextField text] forHTTPHeaderField:@"data[User][password]"];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
NSLog(@"response string: %@ ", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);
}];
[operation start]; …Run Code Online (Sandbox Code Playgroud) 如果我必须附上一个带有后期要求的cookie,该怎么办?我该怎么做?
NSURL *URL = [NSURL URLWithString:addAddressUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
// Set cookie too
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"<URL>"]];
NSDictionary *cookiesDictionary = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
if(cookiesDictionary) {
[request setAllHTTPHeaderFields:cookiesDictionary];
}
Run Code Online (Sandbox Code Playgroud)
如何通过AFNetworking呼叫附加此请求?我已经浏览了AFNetworking的文档,但它没有解释如何在请求中使用其管理器对象设置cookie.
和iF我有点如何在内部将此cookie附加到afnetworking文件,仍然我无法上传图像.我尝试了两种可能的方法:
第一种方式:
-(void)uploadPrescriptionImage :(UIImage *)imagePresc
{
// upload image here on the prescription
/*
Uploading a prescription
URL: <URL>
Params: <PARAMS>
Method: POST
*/
NSData *imageData = UIImageJPEGRepresentation(imagePresc, 1.0);
NSString *orderID = [[NSUserDefaults standardUserDefaults] valueForKey:@"orderId"]; // orderId
NSDictionary *parameters = @{@"docName":prescriptionCell.doctorNameTextField.text,@"patientName":prescriptionCell.patientNameTextField.text,@"orderId" : orderID};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager …Run Code Online (Sandbox Code Playgroud) 我正在尝试拨打服务器.GET调用工作正常并返回正确的json,但是当我尝试执行PUT或POST时,服务器返回错误.
我将服务器设置为接收下一条消息:
method POST
curl -X POST -d "number=NUMBER&name=NAME&lat=32.5713&lon=60.3926" http://server.com/users/
method PUT
curl -X PUT -d "number=USER&name=NAME6&lat=-34.5552&lon=32.3333" http://server.com/users/
Run Code Online (Sandbox Code Playgroud)
如何使用这两种方法调用服务器?
在我的iPhone应用程序中,我使用ASIHTTPRequest API来生成基于cURL的http请求.我听说这个API没有被维护,有时它会崩溃应用程序.我需要使用其他一些API,我可以向我的RESTful API发出cURL请求,该请求将JSON作为响应.
任何人都可以建议我在我的生产应用程序中使用ASIHTTPRequest的最佳替代方案吗?
在我的iPhone应用程序中,我使用AFNetworking将一些数据发送到Web服务并获取一些数据...
这是我的例子,我总是把"假"作为回应,我做错了什么?
NSURL *baseURL = [NSURL URLWithString:@"http://myPath/Iphone/method"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient defaultValueForHeader:@"Accept"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
[udid copy], @"uuid",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"http://myPath/Iphone/method" parameters:params];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFXMLRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
response = [operation responseString];
NSLog(@"response: %@",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [operation error]);
}];
[operation start];
Run Code Online (Sandbox Code Playgroud)
编辑:我在Url中调用的方法返回一个字符串(没有字符串不是false)
[HttpPost]
public bool checkIphone(string uuid)
{
IDictionary<string,object> check = Request.Properties;
uuid = check["uuid"].ToString();
//do …Run Code Online (Sandbox Code Playgroud) ios ×5
afnetworking ×4
iphone ×4
post ×4
rest ×2
cakephp ×1
httprequest ×1
ipad ×1
json ×1
objective-c ×1
put ×1
xcode ×1