From ea17bad2287b5c942f3a8f6ac49d55a7b03d5684 Mon Sep 17 00:00:00 2001 From: guolin Date: Thu, 7 Feb 2013 11:49:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20put?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Network/DOUService.h | 5 +++ .../Sources/Network/DOUService.m | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.h b/DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.h index 756194b..8e0abb6 100644 --- a/DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.h +++ b/DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.h @@ -52,6 +52,11 @@ callback:(DOUReqBlock)block uploadProgressDelegate:(id)progressDelegate; + +- (DOUHttpRequest *)put:(DOUQuery *)query + postBody:(NSString *)body + callback:(DOUReqBlock)block; + - (DOUHttpRequest *)delete:(DOUQuery *)query callback:(DOUReqBlock)block; #endif diff --git a/DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.m b/DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.m index e93a6e1..7e7ac19 100644 --- a/DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.m +++ b/DoubanAPIEngine/DoubanAPIEngine/Sources/Network/DOUService.m @@ -387,6 +387,41 @@ - (DOUHttpRequest *)post:(DOUQuery *)query } +- (DOUHttpRequest *)put:(DOUQuery *)query postBody:(NSString *)body callback:(DOUReqBlock)block { + query.apiBaseUrlString = self.apiBaseUrlString; + __block DOUHttpRequest * req = [DOUHttpRequest requestWithQuery:query completionBlock:^{ + if (block != NULL) { + block(req); + } + }]; + + [req setRequestMethod:@"PUT"]; + [req addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded; charset=UTF-8"]; + + if (body && [body length] > 0) { + + NSError *error = nil; + GDataXMLElement *element = [[[GDataXMLElement alloc] initWithXMLString:body error:&error] autorelease]; + if (!error && element) { + // if body is XML, Content-Type must be application/atom+xml + [req addRequestHeader:@"Content-Type" value:@"application/atom+xml"]; + } + + NSData *objectData = [body dataUsingEncoding:NSUTF8StringEncoding]; + NSString *length = [NSString stringWithFormat:@"%d", [objectData length]]; + [req appendPostData:objectData]; + [req addRequestHeader:@"Content-Length" value:length]; + } + else { + [req addRequestHeader:@"Content-Length" value:@"0"]; + } + + [req setResponseEncoding:NSUTF8StringEncoding]; + [self addRequest:req]; + return req; +} + + - (DOUHttpRequest *)delete:(DOUQuery *)query callback:(DOUReqBlock)block { query.apiBaseUrlString = self.apiBaseUrlString;