Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:41:49+00:00 2026-06-06T22:41:49+00:00

Additional question: Total received data length is now 89973. But converting it into a

  • 0

Additional question:
Total received data length is now 89973. But converting it into a string using the code below returns a (null) string. Solved it: Changed the encoding to NSASCIIStringEncoding and now it works.

NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"Received string: %@", responseString);

Solved question:
I am trying to log into a website and get the returned data.
Everything is working fine and the redirects work like they should.

Also didReceiveData is getting called multiple times, but the strange thing is: The data length is zero. Even if I NSLog the data length from within didReceiveData.

Below are both the .m and .h file and the resulting NSLog information.

I am pretty new to Objective-C and I am trying to fix this for hours now, but I can’t seem to find the cause.

I hope someone can have a look for me.

This is the .m file

#import "SiteConnection.h"

@implementation SiteConnection

@synthesize username;
@synthesize password;

// Other
- (void) printInstanceVars {

}

- (void) getInformation {
    // Enable Network Indicator
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    // Set the request URL
    url = [NSURL URLWithString:@"http://url.com"];

    // Create the request using the URL defined above
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

    // Change User-Agent to a Browser
    [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1" forHTTPHeaderField:@"User-Agent"];

    // Change method to POST
    [request setHTTPMethod:@"POST"];

    // Create POST string
    NSString *requestData = [NSString stringWithFormat:@"data=value"];

    // Append POST string to the request
    [request setHTTPBody: [requestData dataUsingEncoding:NSUTF8StringEncoding]];

    // Initialize the connection
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

    // Start the connection
    [connection start];
}

- (NSURLRequest *)connection:(NSURLConnection *)connection
         willSendRequest:(NSURLRequest *)request
        redirectResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    int statusCode = [httpResponse statusCode];

    // http statuscodes between 300 & 400 is a redirect ...
    if (response && statusCode >= 300 && statusCode < 400) {
        NSLog(@"Redirecting to : %@ (%u)", [request URL], statusCode);
    }

    return request;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [receivedData setLength:0];

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSLog(@"Response: %u (%@)", [httpResponse statusCode], [NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]]);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    NSLog(@"Receiving data... Length: %d", [receivedData length]);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error: %@", [error localizedDescription]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // Hide Network Indicator
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    // Display data length
    NSLog(@"Total received data: %d", [receivedData length]);

    // Convert data into string and display it
    NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"Received string: %@", responseString);
    //NSLog(@"Cookies: %@", [[NSHTTPCookieStorage sharedHTTPCookieStorage] description]);
}

This is the .h file

#import <Foundation/Foundation.h>

@class StatusViewController;

@interface SiteConnection : NSObject {
    NSString *username;
    NSString *password;
    NSURL *url;
    NSMutableData *receivedData;
}

@property NSString *username;
@property NSString *password;

// Other
- (void) printInstanceVars;
- (void) getInformation;

@end

This is the resulting NSLog:

2012-07-02 15:41:12.578 Vodafone[25051:11303] Response: 200 (no error)
2012-07-02 15:41:12.578 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.580 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.580 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.582 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.583 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.585 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.586 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.587 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.590 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.590 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.591 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.592 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.596 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.601 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.605 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.608 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.608 Vodafone[25051:11303] Total received data: 0
2012-07-02 15:41:12.608 Vodafone[25051:11303] Received string: 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:41:50+00:00Added an answer on June 6, 2026 at 10:41 pm

    Do this:

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        if (!receivedData)
        {
            receivedData = [NSMutableData data];
        }
    
        [receivedData appendData:data];
    
        NSLog(@"Receiving data... Length: %d", [receivedData length]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I bumped into an additional question that I needed in regards to this: Using
I was looking at this question: Add additional data to a Highcharts series for
Total newbie question now... Suffice to say, I have searched for a completely noddy
Hi one additional question. I've managed to open my files from excel into R
Referencing Is Facebook an OpenID provider? here. This is kind of an additional question
Additional thanks extend to Daniel Newby for answering my memory usage question (and Martin
Previous question linky: MySQL Left Joins Very kindly we got the following code working:
Here's the question : I have 2 tables , one containing the base data
This is an additional question from this . I have this array containing multiple
What a question! To describe it more clearly: I'm using fragments and my target

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.