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;
}
順便說一句,送它的工作原理空的數據,這意味着它發送請求時。
這實際上是一個偉大的框架,它簡化了一切。謝謝! – 2014-08-30 12:13:41