如何发出POST请求

如何发出POST请求

本文介绍了AFNetworking - 如何发出POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑07/14

正如Bill Burgess在评论中提到的那样,这个问题与版本1.3 AFNetworking 。这里的新人可能已经过时了。

As Bill Burgess mentionned in a comment of his answer, this question is related to the version 1.3 of AFNetworking. It may be outdated for the newcomers here.

我对iPhone开发很新,我我正在使用AFNetworking作为我的服务库。

I'm quite new to iPhone development, and I'm using AFNetworking as my services library.

我正在查询的API是RESTful的,我需要发出POST请求。为此,我尝试使用以下代码:

The API i'm querying is a RESTful one, and I need to make POST requests. To do this, I tried with the following code :

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/login"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"Pass Response = %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Failed Response : %@", JSON);
}];
[operation start];

此代码存在两个主要问题:

There are two main issues with this code :


  • AFJSONRequestOperation 似乎要求 GET 请求,而不是 POST one。

  • 我无法将参数添加到此方法中。

  • AFJSONRequestOperation seems to make a GET request, not a POST one.
  • I can't put parameters to this method.

我也试过这段代码:

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

[httpClient postPath:@"/login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Succes : %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failure : %@", error);
}];

有没有更好的方法来制作我想要的东西来完成它?

Is there a better way to make what I want here to get it done ?

感谢您的帮助!

推荐答案

您可以覆盖请求的默认行为与 AFNetworking 一起使用以作为POST进行处理。

You can override the default behavior of your request being used with AFNetworking to process as a POST.

NSURLRequest *request = [client requestWithMethod:@"POST" path:path parameters:nil];

这假设您已覆盖默认 AFNetworking 设置为使用自定义客户端。如果你不是,我会建议你这样做。只需创建一个自定义类来为您处理网络客户端。

This assumes you have overridden the default AFNetworking setup to use a custom client. If you aren't, I would suggest doing it. Just create a custom class to handle your network client for you.

MyAPIClient.h

#import <Foundation/Foundation.h>
#import "AFHTTPClient.h"

@interface MyAPIClient : AFHTTPClient

+(MyAPIClient *)sharedClient;

@end

MyAPIClient.m

@implementation MyAPIClient

+(MyAPIClient *)sharedClient {
    static MyAPIClient *_sharedClient = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:webAddress]];
    });
    return _sharedClient;
}

-(id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setDefaultHeader:@"Accept" value:@"application/json"];
    self.parameterEncoding = AFJSONParameterEncoding;

    return self;

}

那么你应该可以开启你的网络电话了操作队列没有问题。

Then you should be able to fire off your network calls on the operation queue with no problem.

    MyAPIClient *client = [MyAPIClient sharedClient];
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];

    NSString *path = [NSString stringWithFormat:@"myapipath/?value=%@", value];
    NSURLRequest *request = [client requestWithMethod:@"POST" path:path parameters:nil];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        // code for successful return goes here
        [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];

        // do something with return data
    }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        // code for failed request goes here
        [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];

        // do something on failure
    }];

    [operation start];

这篇关于AFNetworking - 如何发出POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:48