NSData dataWithContentsOfUrl需要javascript

I'm running into a weird issue here.

This is my first time making a database, coding PHP and pulling the output from ios app (Objective c). I was following a tutorial in swift however and I translated the code successfully.

I created a sample data table with 2 columns, queried them with a PHP script and placed it in my webroot directory. The path is http://haiticonnect.epizy.com/GetVotes.php and when I paste it into chrome, the page displays my records. Checking the source code of the page also delivers this exact output.

[{"id":"firstid","post_id":"1"},{"id":"secondid","post_id":"2"}]

My issue is in my objective c code, when I try to grab data with [NSData dataWithContentsOfUrl:myUrl] it always reaches a page that says that the browser does not support javascript. So from the mac, I disable javascript in safari, pasted the url but it shows the correct page.

Can anyone explain why dataWithContentsOfUrl sees javascript?

#define JSON_URL @"http://haiticonnect.epizy.com/GetVotes.php"
NSURL *blogURL = [NSURL URLWithString:JSON_URL];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
const unsigned char *ptr = [jsonData bytes];

for(int i=0; i<[jsonData length]; ++i) {
    unsigned char c = *ptr++;
    NSLog(@"char=%c hex=%x", c, c);
}

I use this algorithm to print every single character it finds, but its only a string saying the my browser does not support javascript.

There are 2 problems with your web page.

  • First it doesn't work with all user agents, so you're going to need to specify the user agent (this is easy).
  • Second it works with a cookie, this is why you're always seeing the javascript page. The first time you visit the page, somehow the javascript will generate a cookie and your browser will send this cookie in all subsequent requests (which is why it will continue to work even with javascript disabled).

Try to clear all cookies for this site, then disable javascript, then retry. You will see that it doesn't work. What you need to do is find out how the javascript generates or requests the cookie, then generate or request the cookie yourself, then make the request with a valid user agent and the cookie.

Once you have the cookie and a valid user agent you can try it with this:

Swift:

let requestURL = URL(string: "http://haiticonnect.epizy.com/GetVotes.php")!
var request = URLRequest(url: requestURL)
request.setValue("valid-user-agent", forHTTPHeaderField: "User-Agent")
request.setValue("cookieName=cookieValue", forHTTPHeaderField: "Cookie")

let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
    guard let data = data else {
        print("Request failed")
        return
    }
    // Handle JSON data here
    let jsonString = String(data: data, encoding: .utf8)
    print(jsonString)
}
task.resume()

Objective-C:

NSURL *requestURL = [NSURL URLWithString:@"http://haiticonnect.epizy.com/GetVotes.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL];
[request setValue:@"valid-user-agent" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"cookieName=cookieValue" forHTTPHeaderField:@"Cookie"];

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (data == nil) {
        NSLog(@"Request failed");
        return;
    }
    // Handle JSON data here
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", jsonString);
}];
[task resume];