Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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