0

I am attempting to recreate this page on iOS. The HTML POST request I require to send the search parameters is as follows:

<FORM ACTION="pw_pub_sched.p_listthislist" METHOD="post">

Using Rob's answer here I was able to create the set up the following HTTP POST request:

@IBAction func testHTTPPost(sender: AnyObject) {
    let request = NSMutableURLRequest(URL: NSURL(string: "https://banner.sbcc.edu/PROD/pw_pub_sched.p_listthislist")!)
    request.HTTPMethod = "POST"
    let postString = "TERM=201730&TERM_DESC=Fall 2016&sel_subj=dummy&sel_day=dummy&sel_schd=dummy&sel_camp=dummy&sel_ism=dummy&sel_sess=dummy&sel_instr=dummy&sel_ptrm=%&level=CR"
    request.HTTPBody = postString.dataUsingEncoding(NSISOLatin1StringEncoding)
    request.HTTPShouldHandleCookies = true
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        (data, response, error) in

        guard error == nil && data != nil else {                                                          // check for fundamental networking error
            print("error=\(error)")
            return
        }

        if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("responseString = \(responseString)")
    }
    task.resume()

}

However I receive a 404 error every time. This is the output:

statusCode should be 200, but is 404
response = Optional(<NSHTTPURLResponse: 0x7f8e398d2410> { URL: https://banner.sbcc.edu/PROD/pw_pub_sched.p_listthislist } { status code: 404, headers {
    Connection = "Keep-Alive";
    "Content-Length" = 215;
    "Content-Type" = "text/html; charset=iso-8859-1";
    Date = "Fri, 27 May 2016 19:23:23 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Oracle-Application-Server-11g";
} })
responseString = Optional(<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /PROD/pw_pub_sched.p_listthislist was not found.</p>
</body></html>
)

When I try to go directly to https://banner.sbcc.edu/PROD/pw_pub_sched.p_listthislist in my browser I also come up with a 404 error. Only by using the submit button on the search page do I successfully view the results page. However I have tried doing a request to the search page directly and also received a 404 error which leads me to believe it is my code. Is there somewhere else I should be sending this request? Why does this page seemingly not exist unless I go through the search page?

Community
  • 1
  • 1
Justin Oroz
  • 614
  • 8
  • 14
  • no, it's probably NOT your code (directly). they're probably referer-checking, or have a cookie/anti-spam sentinel, which is why it works from their search page, but not going directly or from code. your code has to exactly recreate everything that gets sent from their search page, which may mean simulating an initial GET on the page to establish a session and get/extract the antispam token(s). – Marc B May 27 '16 at 19:33
  • Thanks for your reply. It even works when sent from my own HTML document when I save the search page file onto my computer and modify it. So it doesn't seem to matter where the request comes from. Is there a way to check for and/or comply with their requirements? – Justin Oroz May 27 '16 at 19:36
  • Thank you, I will look into that. Which page do i GET on? the results or search page – Justin Oroz May 27 '16 at 19:38
  • replicate exactly what happens when you use the site in your browser: get the search page, extract any necessary cookies/tokens, do your post with those cookies/tokens, and hopefully get results. – Marc B May 27 '16 at 20:02
  • Thanks @MarcB According to [this](http://stackoverflow.com/questions/31464785/swift-how-to-remember-cookies-for-further-http-requests) the cookies are automatically shared across HTTP requests and the first thing I do in my app is to pull from the search page to parse the HTML so I would think the cookies are already in my next request. Does this sound reasonable? Or do I have to manually pull them from somewhere? – Justin Oroz May 27 '16 at 20:13
  • should be ok, as long as you're re-using the same http object. what about hidden form fields? anti-spam tokens? and did you set the http referer for the post request? – Marc B May 27 '16 at 20:14
  • I've added all hidden form fields I could find in the HTML. Is there somewhere else I should look? Where can I find anti spam tokens? I tried using a referrer `request.mainDocumentURL = NSURL(string: "https://banner.sbcc.edu/PROD/pw_pub_sched.p_search")` previously but it didn't work so I removed it. Ill add it again – Justin Oroz May 27 '16 at 20:46
  • also by same HTTP object do you mean `NSURLSession.sharedSession()`? – Justin Oroz May 27 '16 at 20:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113183/discussion-between-justin-oroz-and-marc-b). – Justin Oroz May 27 '16 at 21:09

1 Answers1

0

The problem was I did not send all required parameters with the POST Request.

Thanks to @MarcB for his help

Justin Oroz
  • 614
  • 8
  • 14