0

是否有人已经尝试在不使用包含的(并且很酷的)GCDWebUploader 的情况下为 GET 请求(POST 方法的相同问题)实现处理程序?

我需要服务器响应http://local/download/filename.ext将文件上传到客户端的 GET 请求。

我使请求符合代码“BackgroundSessionManager”(可在此处获得:AFNetworking 2.0 和后台传输),它被发送并触发,不用担心。

我得到服务器端的日志是这样的:

[DEBUG] Did start background task
[DEBUG] Connection received 248 bytes on socket 14
[DEBUG] Connection on socket 14 preflighting request "GET /download/file.ext with 248 bytes body
[DEBUG] Connection on socket 14 processing request "GET /download/file.ext" with 248 bytes body
[EXCEPTION] *** +[NSJSONSerialization dataWithJSONObject:options:error:]: value parameter is nil
[DEBUG] Did close connection on socket 14

我无法弄清楚如何设置处理程序以便不关心从 JSON 解析的不存在的查询。

[webServer addHandlerForMethod:@"GET" path:@"/download" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
        __strong AppDelegate* strongSelf = weakSelf;
        NSLog(@"request for download is %@", request);

        return [strongSelf downloadFile:request];
    }];
4

1 回答 1

3

我删除了此代码以使其正常工作:

/*
 // Add a handler to respond to GET requests
[webServer addDefaultHandlerForMethod:@"GET"
                             requestClass:[GCDWebServerRequest class]
                        asyncProcessBlock:^(GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock) {

                            __strong AppDelegate* strongSelf = weakSelf;
.....
*/

[webServer addHandlerForMethod:@"GET" path:@"/download" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
    __strong AppDelegate* strongSelf = weakSelf;
    NSLog(@"request for download is %@", request);

    return [strongSelf downloadFile:request];
}];

注释掉默认的处理程序,它无论如何都会接管并期待一个 JSON 数据包。

更新

要使用 GCDWebServer 和 NSURLSessions(甚至使用 AFNetworking)实现后台文件传输,对我来说最好的方法是使用 MatchBlock 实例化一个 GET 处理程序,如下所示:

[webServer addHandlerWithMatchBlock:^GCDWebServerRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) {

        if (![requestMethod isEqualToString:@"GET"]) {
            return nil;
        }
        if (![urlPath hasPrefix:@"/download"]) {
            return nil;
        }
        return [[GCDWebServerRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];

    } processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {

        GCDWebServerResponse* response = nil;

        NSString* filePath = [[weakSelf applicationDocumentsDirectory] stringByAppendingPathComponent:[[request.path stringByRemovingPercentEncoding]];
        NSString* fileType = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:NULL] fileType];
        if (fileType) {
            if ([fileType isEqualToString:NSFileTypeRegular]) {
                    // always allow ranges in our requests
                    response = [GCDWebServerFileResponse responseWithFile:filePath byteRange:request.byteRange];
                    [response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"];
            }
        }
        if (response) {
            response.cacheControlMaxAge = 360;
        } else {
            response = [GCDWebServerResponse responseWithStatusCode:kGCDWebServerHTTPStatusCode_NotFound];
        }
        return response;

    }];
于 2014-12-26T00:38:02.140 回答