2014-08-30 160 views
0

我有一個R.E.S.T web服務正在等待收到的GET和POST HTTP請求。將HTTP Post參數從NSDictionary發送到Web服務

我已經創建了一個類來創建,發送和移動這些請求的委託實例。

由於某些原因,我在POST方法中的參數沒有正確發送,結果是請求超時(服務器根本沒有響應)。

我認爲它可能是與格式我是創建請求主體是:

- (NSURLConnection*)requestUrl:(NSString *)url usingMethod:(NSString *)method containingData:(NSDictionary *)data { 
    NSString* dataString = [self createStringFromData:data]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]]; 
    if ([method isEqualToString:HTTP_REQUEST_GET]) 
     request = [self createGetRequest:request withData:dataString]; 
    else if ([method isEqualToString:HTTP_REQUEST_POST]) 
     request = [self createPostRequest:request withData:dataString]; 
    else 
    { 
     NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary]; 
     [errorDetail setValue:@"Http Request Method is Invalid" forKey:NSLocalizedDescriptionKey]; 
     NSError *error = [NSError errorWithDomain:@"com.picomedia.Pico" code:100 userInfo:errorDetail]; 
     [_httpRequestDelegate httpRequestReturnedError:error]; 
     return nil; 
    } 
    //set request time out 
    //show the url and datastring if in debug mode 
    #ifdef DEBUG 
     NSLog(@"Url: %@",[request.URL absoluteString]); 
     NSLog(@"Method: %@", method); 
     NSLog(@"DataString: %@", dataString); 
    #else 
     request.timeoutInterval = 15; 
    #endif 
    // Create url connection and fire request 
    return [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

/** 
Create string from a data dictionary. 
@param data the dictionary to take the data from 
@return the string containing the data 
*/ 
-(NSString*) createStringFromData:(NSDictionary*) data { 
    NSString* dataString = @""; 
    for(NSString* key in data) 
    { 
     NSString* value = (NSString*)[data objectForKey:key]; 
     NSString* keyValuePair = [[[key stringByAppendingString:@"="] stringByAppendingString:[self urlEncodeString:value]] stringByAppendingString:@"&"]; 
     dataString = [dataString stringByAppendingString:keyValuePair]; 
    } 
    if ([dataString length] > 0) 
     dataString = [dataString substringToIndex:[dataString length] - 1]; 
    return dataString; 
} 

/** 
Create an HTTP GET request with data 
@param request the NSMutableURLRequest request 
@param data the string containging key->value pairs of the data. the data should me utf8 and urlencoded 
@return NSMutableURLRequest with the request data 
*/ 
- (NSMutableURLRequest*) createGetRequest:(NSMutableURLRequest*)request withData:(NSString*)data { 
    request.HTTPMethod = HTTP_REQUEST_GET; 
    if (data != nil) 
    { 
     NSString* url = [[[request.URL absoluteString] stringByAppendingString:@"?"]stringByAppendingString:data]; 
     request.URL = [NSURL URLWithString:url]; 
    } 
    return request; 
} 

/** 
Send an Http POST request with a string containg the post data 
@param data the data of the request in string format 
*/ 
- (NSMutableURLRequest*) createPostRequest:(NSMutableURLRequest*)request withData:(NSString*) data { 
    request.HTTPMethod = HTTP_REQUEST_POST; 
    NSString *postLength = [NSString stringWithFormat:@"%d",[data length]]; 

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    // Convert your data and set your request's HTTPBody property 
    NSData *requestBodyData = [data dataUsingEncoding:NSUTF8StringEncoding]; 
    request.HTTPBody = requestBodyData; 
    // Create url connection and fire request 
    return request; 
} 

順便說一句,送它的工作原理空的數據,這意味着它發送請求時。

回答

0

如果你真的想做自己的請求,你應該從做這項工作的AFNetworking部分獲得靈感。該庫正是你想做的事here

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method 
           URLString:(NSString *)URLString 
           parameters:(id)parameters 
            error:(NSError *__autoreleasing *)error 
{ 
    NSParameterAssert(method); 
    NSParameterAssert(URLString); 

    NSURL *url = [NSURL URLWithString:URLString]; 

    NSParameterAssert(url); 

    NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url]; 
    mutableRequest.HTTPMethod = method; 
    mutableRequest.allowsCellularAccess = self.allowsCellularAccess; 
    mutableRequest.cachePolicy = self.cachePolicy; 
    mutableRequest.HTTPShouldHandleCookies = self.HTTPShouldHandleCookies; 
    mutableRequest.HTTPShouldUsePipelining = self.HTTPShouldUsePipelining; 
    mutableRequest.networkServiceType = self.networkServiceType; 
    mutableRequest.timeoutInterval = self.timeoutInterval; 

    mutableRequest = [[self requestBySerializingRequest:mutableRequest withParameters:parameters error:error] mutableCopy]; 

    return mutableRequest; 
} 

相比,還有這取決於你想用你的請求編碼requestBySerializingRequest:withParameters:error:幾種實現。

我希望這會有所幫助。

+0

這實際上是一個偉大的框架,它簡化了一切。謝謝! – 2014-08-30 12:13:41

相關問題