0

我正在做一个项目,我需要通过 RESTful Web 服务从服务器获取数据。

服务器端的人已经实现了一些 Web 服务。我需要使用这些方法,但我不确定使用“Get”或“POST”哪个 http 方法。

如果我使用“GET”作为 http 方法并且如果服务器 Web 服务是在“POST”中实现的,那么我会得到 404 或类似的 http 错误代码。

有没有办法找出为 Web 服务器 API 实现的 http 方法类型?

现在我正在使用 BOOL 标志来确定要使用的 Http 方法。旗帜是从外面设置的。

NSMutableURLRequest *urlRequest = nil;

if (_usePostMethod)
{
    urlRequest = [[NSMutableURLRequest alloc] initWithURL:self.serverURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0f];;
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:[restMessage.message dataUsingEncoding:NSUTF8StringEncoding]];
}
else
{
    urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.serverURL, restMessage.message]]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0f];
    [urlRequest setHTTPMethod:@"GET"];
}

我想摆脱这个BOOL _usePostMethod变量。

4

2 回答 2

1

@Omkar Ramtekkar 没有任何方法可以知道...在调用 Web 服务之前,您必须了解 http 方法,您必须使用 Post ot Get..

于 2013-06-24T07:36:51.363 回答
0

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process.

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions:

  - Annotation of existing resources;
  - Posting a message to a bulletin board, newsgroup, mailing list,
    or similar group of articles;
  - Providing a block of data, such as the result of submitting a
    form, to a data-handling process;
  - Extending a database through an append operation.
于 2013-06-24T06:06:43.900 回答