IOS如何POST GET DELETE PUT Rest Api

标签 ios api rest

我正在构建连接到 Rest API 的应用程序,到目前为止我只使用以下代码发出了 GET 请求:

//Start login process
NSString *emailstring = email.text;
NSString *passstring = pass.text;

// Create the URL from a string.
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myserver.com/Rest/API/users?format=json&email=%@&password=%@",emailstring,passstring]];
NSLog(@"%@",url);

// Create a request object using the URL.
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// Prepare for the response back from the server    
NSHTTPURLResponse *response = nil;
NSError *error = nil;

// Send a synchronous request to the server (i.e. sit and wait for the response)
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Reponse from web:%@", response);

// Check if an error occurred    
if (error != nil) {
    NSLog(@"%@", [error localizedDescription]);
    // Do something to handle/advise user.

    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Login error"
                                                      message:@""
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];

}

else {

    // Convert the response data to a string.
    NSString *responseString = [[NSString alloc] initWithData:responseData  encoding:NSUTF8StringEncoding];

    // View the data returned - should be ready for parsing.
    NSLog(@"%@", responseString);

    // Add data to a Plist file for next time
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"login.plist"];

    NSArray *values = [[NSArray alloc] initWithObjects:emailstring,passstring,@"IDtest",nil];
    [values writeToFile:path atomically:YES];
    [values release];


    [self dismissModalViewControllerAnimated:YES];
}

此代码仅适用于 GET 请求。我看到有很多框架(例如 RestKit,......)。但我对其他请求有点迷失了!那么对 IOS 应用程序发出 POST DELETE PUT 请求的最佳解决方案是什么?

最佳答案

这是类似的代码,但使用类 NSMutableRequest。您可以设置httpbody等参数与服务器进行通信。

查看文档:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html

要发布内容,只需输入 setHTTPMethod:@"POST"并使用 setHTTPBody 分配要发布的数据:

关于IOS如何POST GET DELETE PUT Rest Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12047717/

相关文章:

iphone - "No provisioning iOS device connected"

java - 处理:如何检查 JSON 对象是否已绑定(bind)

javascript - 通过单击按钮简单地从 soundcloud 播放声音

java - 404/503 错误的 Json 响应

java - @ExceptionHandler 没有被调用测试

iOS 无法将 "Configure one of the app’ s 套接字用于 VoIP 使用“用于后台模式

ios - 如果我通过开发人员签名而不是分发证书来发布 iOS,会发生什么情况?

ios - 自定义 navigationItem 的 searchController,但不适用于 iOS 11

java - 无法从 Facebook 商务管理平台创建 WhatsApp 帐户

java - Jersey,JAXB 并获取一个扩展抽象类的对象作为参数