开发者

How to download source code of url that has no file extension?

开发者 https://www.devze.com 2023-02-01 00:19 出处:网络
As of right now I\'m using this code below to download files to get there source code: NSString *stringUrl = @\"http://example.com/hello/goodbye.html\";

As of right now I'm using this code below to download files to get there source code:

    NSString *stringUrl = @"http://example.com/hello/goodbye.html";
NSURL *finalURL = [NSURL URLWithString:开发者_运维知识库[stringUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
self.source = [NSString stringWithContentsOfURL:finalURL encoding:NSUTF8StringEncoding error:nil];

Don't get me wrong, it works great but it doesn't work at all if the url doesn't have an extension like so:

NSString *stringUrl = @"http://example2.com/something/";
NSURL *finalURL = [NSURL URLWithString:[stringUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
self.source = [NSString stringWithContentsOfURL:finalURL encoding:NSUTF8StringEncoding error:nil];

How would I get the html source code of something that the extension isn't defined? Hopefully someone can help me with this because my whole project relies on getting the source from url's with no extension. Thanks


It should work just as if there was a file defined. The GET url is merely a request to the webserver. The server is allowed to return whatever content of the "file" it wants to.

In other words: the complexity of handling the strange URLs like that need not be a part of your downloading code (so long as you process redirect requests). That's complexity for the server-side.

EDIT:

If you're trying to get the PHP source of the page (or whatever server-side language the web application is written in), you are outta luck. All of that code is processed server side and is never sent to the client at all.

EDIT2:

If you're trying to turn something like http://example2.com/something/ into http://example2.com/something/whatever-file-the-webserver-used.XXX, then you are also out of luck. Nobody ever said the webserver ever had to hand that off to a file at all -- again, the URL just gets turned into a GET request to the server. The server is free to generate the response however it wishes, and it need not even consult a specific filesystem file to generate the result (it could be generated completely in memory by the webserver itself, and never touch the filesystem at all). The system need not even know the concept of a file and still be able to implement HTTP itself just fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消