Skip to content

Instantly share code, notes, and snippets.

@moralez
Created January 26, 2012 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moralez/04c477231bae3c27b7dc to your computer and use it in GitHub Desktop.
Save moralez/04c477231bae3c27b7dc to your computer and use it in GitHub Desktop.
Download Query
- (void)doDownloadQuery:(JsonQueryParam *)queryParam withRequest:(id)request andProgressDeleate:(id)progressDelegate
{
param = [queryParam retain];
NSData *postData = nil;
NSString *postLength = nil;
NSString *method = nil;
NSArray *command = nil;
NSString *documentsDir = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *downloadPath = [documentsDir stringByAppendingString:[NSString stringWithFormat:@"/response.txt", [request integerForKey:@"id"]]];
if ([request isKindOfClass:[NSArray class]])
command = [request retain];
else
command = [[NSArray alloc] initWithObjects:request, nil];
SBJSON *json = [[SBJSON alloc] init];
NSString *post = [json stringWithObject:command error:nil];
[command release];
[json release];
postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
postLength = [NSString stringWithFormat:@"%d", [postData length]];
method = @"POST";
ASIHTTPRequest *urlRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[queryParam url]]];
[urlRequest addRequestHeader:@"Content-Type" value:@"application/x-json"];
[urlRequest setTimeOutSeconds:20.0f];
[urlRequest setRequestMethod:method];
// [urlRequest setDidReceiveResponseHeadersSelector:@selector(requestDidReceiveResponseHeader:)];
if (nil != postData)
{
[urlRequest appendPostData:postData];
}
if (nil != postLength)
{
[urlRequest setPostLength:[postData length]];
}
[urlRequest setUsername:[param userName]];
[urlRequest setPassword:[param password]];
[urlRequest setValidatesSecureCertificate:NO];
[urlRequest setDelegate:self];
[urlRequest setDownloadDestinationPath:downloadPath];
[urlRequest setDownloadProgressDelegate:progressDelegate];
[urlRequest setNumberOfTimesToRetryOnTimeout:3];
[urlRequest setTemporaryFileDownloadPath:[downloadPath stringByAppendingString:@".download"]];
[urlRequest setAllowResumeForFileDownloads:YES];
[urlRequest setShowAccurateProgress:YES];
[urlRequest setShouldPresentCredentialsBeforeChallenge:YES];
[urlRequest startAsynchronous];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment