2012-05-02 27 views
9

मैं आईफोन ऐप विकसित कर रहा हूं और मैन्युअल रूप से POST अनुरोधों का निर्माण कर रहा हूं। वर्तमान में, इसे भेजने से पहले जेएसओएन डेटा को संपीड़ित करने की आवश्यकता है, इसलिए सर्वर को बताने के लिए सामग्री को संपीड़ित करना है। सामग्री प्रकार हेडर को gzip पर सेट करना स्वीकार्य नहीं हो सकता क्योंकि सर्वर JSON डेटा की अपेक्षा करता है। मैं पारदर्शी समाधान की तलाश में हूं, कुछ ऐसा है जो कुछ हेडर जोड़ने के लिए है जो JSON डेटा को gzip में संपीड़ित किया गया है।gziped सामग्री के साथ HTTP POST अनुरोध कैसे भेजें?

मुझे पता है, क्लाइंट सर्वर को यह बताने का मानक तरीका है कि क्लाइंट एन्कोडिंग स्वीकार करता है, लेकिन आपको पहले एन्कोडिंग हेडर स्वीकार करने के साथ GET अनुरोध करना होगा। मेरे मामले में, मैं पहले से एन्कोड किए गए डेटा को पोस्ट करना चाहता हूं।

+0

मिला: http://serverfault.com/questions/56700/is-it-possible-to-enable-http-compression-for-requests – Centurion

उत्तर

-1

कुछ सामान्य तरीकों को लागू करना जैसे उचित हेडर सेट करना और आपकी सहायता करना आपकी मदद कर सकता है।

// constructing connection request for url with no local and remote cache data and timeout seconds 
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:callingWebAddress]];// cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:timoutseconds]; 
[request setHTTPMethod:@"POST"]; 

NSMutableDictionary *headerDictionary = [NSMutableDictionary dictionary]; 
[headerDictionary setObject:@"application/json, text/javascript" forKey:@"Accept"]; 
[headerDictionary setObject:@"application/json" forKey:@"Content-Type"]; 

//Edit as @centurion suggested 
[headerDictionary setObject:@"Content-Encoding" forKey:@"gzip"]; 
[headerDictionary setObject:[NSString stringWithFormat:@"POST /Json/%@ HTTP/1.1",method] forKey:@"Request"]; 
[request setAllHTTPHeaderFields:headerDictionary]; 

// allocation mem for body data 
self.bodyData = [NSMutableData data]; 

[self appendPostString:[parameter JSONFragment]]; 

// set post body to request 
[request setHTTPBody:bodyData]; 

NSLog(@"sending data %@",[[[NSString alloc] initWithData:bodyData encoding:NSUTF8StringEncoding]autorelease]); 

// create new connection for the request 
// schedule this connection to respond to the current run loop with common loop mode. 
NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
//[aConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; 
self.requestConnenction = aConnection; 
[aConnection release]; 
+0

हम्म, मैं किसी भी एन्कोडिंग संबंधित शीर्ष नहीं दिखते। मैं बस googled, आपको "सामग्री-एन्कोडिंग: gzip" शीर्षलेख सेट करने की आवश्यकता है – Centurion

+0

यहां gzipping के बारे में कुछ भी नहीं है ... –

16

एक Obj सी gzip आवरण, उदाहरण के NSData+GZip के लिए शामिल करें और इसका इस्तेमाल अपने NSURLRequest के शरीर एन्कोड करने के लिए। इसके अनुसार Content-Encoding तदनुसार सेट करना याद रखें, इसलिए वेबसर्वर आपके अनुरोध का इलाज कैसे करेगा।

NSData *requestBodyData = [yourData gzippedData]; 
NSString *postLength = [NSString stringWithFormat:@"%d", requestBodyData.length]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"]; 
[request setHTTPBody:requestBodyData]; 
संबंधित मुद्दे