Read NSURLresponse

or azran picture or azran · Nov 7, 2011 · Viewed 36.2k times · Source

i am sending an object to this adrees : https://sandbox.itunes.apple.com/verifyReceipt

with NSUrlconnection and i am trying to read it with this delegate method :

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response like this :

NSlog(@"%@",response); i am getting this code :

<NSHTTPURLResponse: 0x7d2c6c0> i need to get a string somehow. how can i read it?

Answer

James Webster picture James Webster · Nov 8, 2011

I wrote this answer to another question, but I think it will help you. Have a look in particular at the methods

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

and

-(void) connectionDidFinishLoading:(NSURLConnection *)connection


-(void) requestPage
{
    NSString *urlString = @"http://the.page.you.want.com";
    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f];


    responseData = [[NSMutableData alloc] init];
    connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
    delegate = target;
}


-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{   
    if ([response isKindOfClass:[NSHTTPURLResponse class]])
    {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
        //If you need the response, you can use it here
    }
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [responseData release];
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == adCheckConnection)
    {
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

        //You've got all the data now
        //Do something with your response string


        [responseString release];
    }

    [responseData release];
    [connection release];
}