Re: Alternative to stringWithContentsOfURL
Re: Alternative to stringWithContentsOfURL
- Subject: Re: Alternative to stringWithContentsOfURL
- From: John Pannell <email@hidden>
- Date: Tue, 27 Oct 2009 17:40:26 -0600
Hi James-
From your description ("Internal Error 500") it sounds like the
request you are sending is not working when submitted to the server.
Make sure any strings used to form the URL for the request are passed
through stringByAddingPercentEscapesUsingEncoding:
What follows is likely not your issue, but it is something I've
encountered in the past (and could be good for the archives). Some
web servers are configured to compress the reply (i.e. zip/gzip) for
transmission, and then the client will decompress and display.
NSString is not a client that is prepared to do this, however. Here
is some old code:
NSString *responseString;
NSError *error;
if(responseString = [NSString stringWithContentsOfURL:[NSURL
URLWithString:@"http://www.mywebsite.com"] usedEncoding:&enc
error:&error]){
// do whatever
} else {
// no string ?!
}
The code above will not work with webservers that compress their reply
data. NSURLConnection takes care of this for you, so you need to work
the data it returns back into a string. Here is some newer code:
NSString *responseString;
NSURLResponse* resp;
NSError *error;
NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL
URLWithString:@"http://www.mywebsite.com"]];
NSData* data = [NSURLConnection sendSynchronousRequest:req
returningResponse:&resp error:&error];
if(responseString = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding]){
// do whatever
} else {
// no string ?!
}
I discovered this when moving to a new host, which broke my reg code
verification check with my server. My new host utilized gzip
compression on outgoing content. The browser checked out just fine,
because it knows to read the response headers and decompress the
content. Note that the synchronous request outlined above will hang
until timeout if your server is not responding. You can use the async
+delegate methods if something more robust is required.
Hope this helps!
John
http://www.positivespinmedia.com
On Oct 27, 2009, at 9:35 AM, James Lin wrote:
The sticky point right now is: the same url string used with
stringWithContentsOfURL works perfectly when accessed using a browser.
Which means my php script is in perfect working order.
But when accessed using stringWithContentsOfURL, then it doesn't work!
I can tell my ISP is not going to take up the responsibility for
this one, because it works in a browser...
That's why i am asking for alternatives to stringWithContentsOfURL...
James
On 2009/10/27, at 下午11:25, Roland King wrote:
On 27-Oct-2009, at 11:16 PM, Dave Carrigan wrote:
On Oct 27, 2009, at 8:11 AM, James Lin wrote:
Hi all,
I am still having the mysterious error of "Internal Error 500"
message returned from stringWithContentsOfURL.
If "Internal Error 500" is the contents of the string after you
create it using +stringWithContentsOfURL, then the problem is with
the server, and changing to use something else is not going to
change that fact.
Exactly. You need to go find out what is being sent to the server
when you use that method and why it's erroring. You're missing a
parameter or you encoded something wrong or there's a cookie you
need to set or something. There is nothing wrong with the cocoa
method, you're either not setting it up right, or you need to add
more stuff or it's possible that the request does need things that
stringWithContentsOfURL: can't add, but at this point you haven't
figured that out and don't know what, if anything they are, so no
other method will help you.
The error log from the server would be of use.
HTTP is simple. Look at what you're sending out with some kind of
snoop between your non-cocoa way and your cocoa way, the answer
lies there.
_______________________________________________
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