dongyu4455 2017-06-18 15:12
浏览 90

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.

  • 写回答

1条回答 默认 最新

  • dongxuan1660 2017-06-18 15:59
    关注

    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];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘