0

I'm trying to make an app that fetches some text from a website. But I got an error:

Error Domain=NSCocoaErrorDomain Code=256 "The file “XXX.aspx” couldn’t be opened." UserInfo={NSURL=...}

I can open the website using my browser. I think the problem is about the aspx file. So how do I get the html from a aspx file? Here is the code I used:

Here is the code I used:

let myURLString = "https://https://nutrition.sa.ucsc.edu/shortmenu.aspx?sName=UC+Santa+Cruz+Dining&locationNum=25&locationName=Porter+Kresge+Dining+Hall&naFlag=1&WeeksMenus=UCSC+-+This+Week%27s+Menus&myaction=read&dtdate=9%2f2%2f2019"

guard let myURL = URL(string: myURLString) else {
            print("Invalid URL")
            return
        }

        do {
            let myHTMLString = try String(contentsOf: myURL, encoding: .ascii)
            print("HTML : \(myHTMLString)")
        } catch let error {
            print("Error: \(error)")
        }
1
  • I get Server Error in '/' Application error with your link? Can you check it on browser? Commented Sep 4, 2019 at 8:02

1 Answer 1

2

You can get html from your link like this. But i think your link is broken. It gives server error.


let myURLString = "https://nutrition.sa.ucsc.edu/shortmenu.aspx?sName=UC+Santa+Cruz+Dining&locationNum=25&locationName=Porter+Kresge+Dining+Hall&naFlag=1&WeeksMenus=UCSC+-+This+Week%27s+Menus&myaction=read&dtdate=9%2f2%2f2019"


let url = URL(string: myURLString)!
var request = URLRequest(url: url)
request.httpMethod = "GET"

let session = URLSession.shared
let task = session.dataTask(with: request) {
    (
    data, response, error) in

    let dataString = String(data: data!, encoding: .utf8)
    print(dataString)

}
task.resume()

Sign up to request clarification or add additional context in comments.

5 Comments

This is a university domain. Maybe there is some restrictions for attempt by unauthorized people.
There is no login kinda thing on that website. Maybe I'm using the wrong way to request the content?
Maybe. But i think if you cannot see any error on this link probably there is a firewall on your network. Can you check with different network?
I can view the website on browser using the same network. I'm thinking is there any way I can use the webkit or something actually load the website in background and convert it to html code?
ok I figured it out. I have to request the main page first and then this one. But thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.