Re: UTF-8 string encoding bug in stringWithContentsOfURL?
Re: UTF-8 string encoding bug in stringWithContentsOfURL?
- Subject: Re: UTF-8 string encoding bug in stringWithContentsOfURL?
- From: "Doug Knowles" <email@hidden>
- Date: Sun, 8 Jul 2007 13:49:27 -0400
Hi, Ken,
I've seen this, and got burned by the followup, too.
I was getting the same errors from stringWithContentsOfURL:, and was advised
on this list that the ...WithContentsOfURL methods will sometimes fail in
this fashion if the server if gzip'ing the results before sending them back.
This was very confusing, because my alternate techniques for examining the
URL responses (typically "curl" on the command line) handle the gzip'd
responses invisibly.
Following the advice I got, I changed my code to use NSURLDownload (which
turned out to be relatively painless), and my erroneous UTF-8 errors went
away.
One caveat: I am doing my downloading on separate threads, and NSURLDownload
turned up a separate problem wherein it invokes the downloadDidFinish:
callback on the main thread (instead of the invoking thread, as it's
supposed to) when the response is an error. If you're doing all your work
on the main thread, this won't affect you, but otherwise you might want to
go straight to NSURLConnection to fetch your data.
Hope this helps; let me know if I can clarify any of this.
Doug K;
On 7/8/07, Ken Tozier <email@hidden> wrote:
Hi
I'm writing up a few image search functions for popular search sites
(Google, Yahoo, MSN) and ran into a problem with NSString's
stringWithContentsOfURL at MSN. Doing a "view source" on the results
of an image search indicates that the search results are encoded as
utf-8 (charset=utf-8) but stringWithContentsOfURL reports the error:
"File could not be opened using text encoding Unicode (UTF-8)."
Both Google and Yahoo define charset like this "charset=UTF-8" could
the fact that MSN defines it as "charset=utf-8" (note the lowercase)
be confusing NSString?
Next I tried "stringWithContentsOfURL :usedEncoding:error:" leaving it
up to NSString to figure out what type of encoding to use but got the
error: "The file could not be opened using specified text encoding."
Safari seems to have no problem interpreting the search result so
there must be a way to do it. Does anyone know if there is any other
way to get the url contents into an NSString?
For the adventurous, here's a prepackaged MSN image query that works
in Safari but not with stringWithContentsOfURL
NSError *err = nil;
NSString *result = [NSString stringWithContentsOfURL: [NSURL
urlWithString: @"http://search.msn.com/images/results.aspx?q=white
+tiger"]
encoding:
NSUTF8StringEncoding
error: &err];
TIA
Ken
______________________________ _________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists .apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden