Re: NSURLRequest returning bizarre page
Re: NSURLRequest returning bizarre page
- Subject: Re: NSURLRequest returning bizarre page
- From: "Manoah F. Adams" <email@hidden>
- Date: Mon, 10 Jun 2013 20:11:27 -0700
Hello,
[this message updated; previous message failed to get posted to the
list]
I noticed that the garbling is always "^M\n" -- indicating to me
there is a newline/linebreak encoding issue.
(especially since the server is a Microsoft monster).
2 ideas in order of likelihood:
=== (1) ===
Try setting the NSURLRequest to request *only* "text/plain",
(Noticing how safari gets back "text/plain" mime type, but your cocoa
app got back a (garbled) HTML.
The result you got looks like untested code in the server for
wrapping plain text into html format.
I did some experimenting...
Try this to check :
// for testing use a header echo service ...
NSURL* tleAddress = [NSURL URLWithString:@"http://scooterlabs.com/
echo.xml"];
// Must use NSMutableURLRequest .. .see
http://stackoverflow.com/questions/4809047/nsurlrequest-setting-the-
http-header
NSMutableURLRequest* tleRequest = [[[NSMutableURLRequest alloc]
initWithURL:tleAddress]
autorelease];
[tleRequest setValue:@"text/plain" forHTTPHeaderField:@"Accept"]; //
no trailing colon (:)
NSURLResponse* tleResponse = nil;
NSError* tleError = nil;
NSData* tleData = [NSURLConnection sendSynchronousRequest:tleRequest
returningResponse:&tleResponse error:&tleError];
NSString* tleString = [[[NSString alloc] initWithData:tleData
encoding:NSUTF8StringEncoding] autorelease];
// for testing...
NSLog(tleString); // look for the value inside the <accept></accept>
tags
=== (2) ===
try changing your NSUTF8StringEncoding to NSASCIIStringEncoding
like so...
NSString* tleString = [[[NSString alloc] initWithData:tleData
encoding: NSASCIIStringEncoding] autorelease];
-- Manoah Adams
On Jun 10, 2013, at 17:06 , Trygve Inda wrote:
I am using the following code (url changed, but it is a .txt file)
NSString* tleAddress = @"http://www.somesite.com/somefile.txt";
NSURLRequest* tleRequest = [NSURLRequest requestWithURL:[NSURL
URLWithString:tleAddress] cachePolicy:0 timeoutInterval:5.0];
NSURLResponse* tleResponse = nil;
NSError* tleError = nil;
NSData* tleData = [NSURLConnection sendSynchronousRequest:tleRequest
returningResponse:&tleResponse error:&tleError];
NSString* tleString = [[[NSString alloc] initWithData:tleData
encoding:NSUTF8StringEncoding] autorelease];
This works fine on my test systems and the vast majority of my
customer's
systems. I am able to open the URL in Safari and see that it is a
raw text
file.
tleString comes back with the correct text.
However, on some customer's systems, despite NSError not showing
anything
wrong, I get:
^M\n<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"^M\n"http://www.w3.org/TR/html4/loose.dtd">^M
\n<html>^M\n<h
ead>^M\n<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">^M\n<title>Untitled Document</title>^M\n<script
language="javascript">^M\n var loc = escape
(document.location.href);^M\n
//var ref = escape(document.referrer);^M\n var newLoc =
"/redirect.asp?loc=" + loc;^M\n
window.location.href(newLoc);^M\n</script>^M\n</head>^M\n^M
\n<body>^M\n</bod
y>^M\n</html>^M
I have no idea where this is coming from as it is certainly not in
the file.
The customer is not using a proxy or firewall and it happens in two
different locations (so it could be related to the machine rather
than the
internet).
Ideas?
Thanks,
Trygve
_______________________________________________
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:
40federaladamsfamily.com
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