No data from my own domain


#1

Hi all,

Please, can anyone explain why I am not receiving data from my own web-domain? When I use the URL from the book it works fine, but when I try to fetch the index.htm from my own domain the result is as follows:

This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001 [Switching to process 58056 thread 0x0] 2012-03-06 14:33:06.721 Callbacks[58056:707] received 1448 bytes 2012-03-06 14:33:06.722 Callbacks[58056:707] received 1448 bytes 2012-03-06 14:33:06.752 Callbacks[58056:707] received 2896 bytes 2012-03-06 14:33:06.761 Callbacks[58056:707] received 3848 bytes 2012-03-06 14:33:06.793 Callbacks[58056:707] received 1448 bytes 2012-03-06 14:33:06.793 Callbacks[58056:707] received 2896 bytes 2012-03-06 14:33:06.801 Callbacks[58056:707] received 2400 bytes 2012-03-06 14:33:06.802 Callbacks[58056:707] received 4344 bytes 2012-03-06 14:33:06.833 Callbacks[58056:707] received 1448 bytes 2012-03-06 14:33:06.833 Callbacks[58056:707] received 5296 bytes 2012-03-06 14:33:06.841 Callbacks[58056:707] received 5296 bytes 2012-03-06 14:33:06.841 Callbacks[58056:707] received 2896 bytes 2012-03-06 14:33:06.842 Callbacks[58056:707] received 2896 bytes 2012-03-06 14:33:06.873 Callbacks[58056:707] received 2400 bytes 2012-03-06 14:33:06.873 Callbacks[58056:707] received 2896 bytes 2012-03-06 14:33:06.874 Callbacks[58056:707] received 3365 bytes 2012-03-06 14:33:06.874 Callbacks[58056:707] Got it all 2012-03-06 14:33:06.875 Callbacks[58056:707] string has 0 characters 2012-03-06 14:33:06.875 Callbacks[58056:707] The whole string is (null)
My main.m:

//
//  main.m
//  Callbacks
//
//  Created by Lucas Uylings on 05-03-12.
//  Copyright (c) 2012 Lucas Information Services. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Logger.h"

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        Logger *logger=[[Logger alloc] init];
        //NSURL *url=[NSURL URLWithString:@"http://www.gutenberg.org/cache/epub/205/pg205.txt"];
        NSURL *url=[NSURL URLWithString:@"http://www.go2me.nl/index.htm"];
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        __unused NSURLConnection *fetchconn= [[NSURLConnection alloc] initWithRequest:request delegate:logger startImmediately:YES];
         [[NSRunLoop currentRunLoop] run]; 
        
    }
    return 0;
}

The Logger.h:

[code]//
// Logger.h
// Callbacks
//
// Created by Lucas Uylings on 05-03-12.
// Copyright © 2012 Lucas Information Services. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Logger : NSObject
{
NSMutableData *incomingdata;
}
// - (void) sayOuch:(NSTimer *)t;

@end[/code]
And finally my Logger.m:

[code]// Logger.m
// Callbacks
//
// Created by Lucas Uylings on 05-03-12.
// Copyright © 2012 Lucas Information Services. All rights reserved.
//

#import “Logger.h”

@implementation Logger
/*

  • (void) sayOuch:(NSTimer *)t
    {
    NSLog(@“Ouch”);
    }
    */

//Called each time a chunk of data arrives

  • (void) connection: (NSURLConnection *) connection
    didReceiveData:(NSData *)data
    {
    NSLog(@“received %lu bytes”, [data length]);
    // Create a mutabel data if it does not already exist
    if (!incomingdata)
    {
    incomingdata=[[NSMutableData alloc] init];
    }
    [incomingdata appendData:data];
    }

// Called when the last chunk has been processed

  • (void) connectionDidFinishLoading: (NSURLConnection *) connection
    {
    NSLog(@“Got it all”);
    NSString *string=[[NSString alloc] initWithData:incomingdata encoding:NSUTF8StringEncoding];
    incomingdata=nil;
    NSLog(@“string has %lu characters”, [string length]);
    NSLog(@“The whole string is %@”, string);
    }

// Called if the fetch fails

  • (void) connection:(NSURLConnection *) connection
    didFailWithError:(NSError *)error
    {
    NSLog(@“connection failed: %@”, [error localizedDescription]);
    incomingdata=nil;
    }
    @end
    [/code]

#2

I looked at your HTML. Here is the important line:

You are not using UTF-8, your website is using a proprietary Microsoft character encoding. When you create your string, use the correct encoding:

NSString *string = [[NSString alloc] initWithData:incomingData encoding:NSWindowsCP1252StringEncoding];


#3

Hi Aaron,

Thanks for your reply. You are right (of course). It works fine now. Your book is excellent.

Regards, Lucas


#4

Very usefull, helped me out too. Thanks :slight_smile:


#5

+1
Thanks Aaron!