NSURLConnection Usage

– (void)sendRequestToServer:(NSURL *)url httpBody:(NSString *)methodCallString
{

    NSMutableURLRequest *xmlrpcRequest =[NSMutableURLRequest requestWithURL:url
                                                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                            timeoutInterval:60.0];
    [xmlrpcRequest setHTTPMethod:@”POST”];
    [xmlrpcRequest setValue:@”text/xml” forHTTPHeaderField:@”Content-Type”];
   
    // set the HTTPBody with the method call string as bytes
    const char *utfString = [methodCallString UTF8String];
   
    NSString *utfStringLenString = [NSString stringWithFormat:@”%u”, strlen(utfString)];
   
    [xmlrpcRequest setHTTPBody:[NSData dataWithBytes: utfString length:strlen(utfString)]];
   
    // Set the Content-Length header
    [xmlrpcRequest setValue:utfStringLenString forHTTPHeaderField:@”Content-Length”];
   
    // create the connection with the request and start loading the data
    if (!isConnectionBusy) {
        isConnectionBusy = YES;
        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:xmlrpcRequest delegate:self];
       
        if (theConnection) {
            responseData = [[NSMutableData data] retain];
        } else {
            NSLog(@”Connection Failed!”);
        }
    }
}

– (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if (responseData != NULL)
        [responseData setLength:0];
}

– (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
    //    NSString *responseString = [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] autorelease];
}

– (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    isConnectionBusy = NO;

    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [responseData release];
       
    // inform the user
    NSLog(@”Connection failed! Error – %@ %@”,
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

– (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    isConnectionBusy = NO;
   
    NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];

    [connection release];
    connection = nil;
    [responseData release];

    [self parseData:responseString];
    [responseString release];
    // release the connection, and the data object
}

-(NSURLRequest *)connection:(NSURLConnection*) connection
            willSendRequest:(NSURLRequest*)    request
           redirectResponse:(NSURLResponse*) redirectResponse
{
    //NSLog(@”connection willSendRequest redirectResponse”);

    NSURLRequest *newRequest = request;
    if (redirectResponse)
    {
        newRequest=nil;
    }
    else
    {
    }
    return newRequest;
}

– (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}

Blogged with the Flock Browser
Advertisements