【发布时间】:2016-09-26 05:41:22
【问题描述】:
我正在尝试在 iOS 上重新创建 this 页面。我需要发送搜索参数的 HTML POST 请求如下:
<FORM ACTION="pw_pub_sched.p_listthislist" METHOD="post">
使用 Rob's answer here 我能够创建设置以下 HTTP POST 请求:
@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()
}
但是,我每次都会收到 404 错误。这是输出:
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>
)
当我尝试在浏览器中直接访问 https://banner.sbcc.edu/PROD/pw_pub_sched.p_listthislist 时,我也遇到了 404 错误。只有使用搜索页面上的提交按钮,我才能成功查看结果页面。但是,我尝试直接向搜索页面发出请求,但也收到了 404 错误,这让我相信这是我的代码。我应该在其他地方发送此请求吗?除非我通过搜索页面,否则为什么此页面似乎不存在?
【问题讨论】:
-
不,这可能不是您的代码(直接)。他们可能正在检查推荐人,或者有一个 cookie/反垃圾邮件哨兵,这就是为什么它可以在他们的搜索页面上工作,但不能直接或来自代码。您的代码必须准确地重新创建从搜索页面发送的所有内容,这可能意味着在页面上模拟初始 GET 以建立会话并获取/提取反垃圾邮件令牌。
-
感谢您的回复。当我将搜索页面文件保存到我的计算机上并对其进行修改时,它甚至可以从我自己的 HTML 文档发送。所以请求来自哪里似乎并不重要。有没有办法检查和/或遵守他们的要求?
-
谢谢,我会调查的。我在哪个页面上获取?结果或搜索页面
-
准确复制您在浏览器中使用该网站时发生的情况:获取搜索页面,提取任何必要的 cookie/令牌,使用这些 cookie/令牌发布您的帖子,并希望得到结果。
-
谢谢@MarcB 根据this,cookie 会自动在 HTTP 请求之间共享,我在我的应用程序中做的第一件事是从搜索页面中提取来解析 HTML,所以我认为 cookie已经在我的下一个请求中。这听起来合理吗?还是我必须从某个地方手动拉出它们?