好的,所以我工作的地方在线发布每周工作时间表,基本上,我想写一个程序(我最终将变成一个Android应用程序,所以我用Java编写程序)发送数据到网站(我的用户名和密码),然后一旦登录,从网站上获取时间表。一旦我抓住了时间表,我就会针对事件进行解析(我打算将其自动添加到我的手机日历中作为事件)。
无论如何,我在这方面遇到了一些麻烦。所以,基本上,我做了一个小的Java函数来发送POST数据到网站,它看起来像这样:
public void test1 (){
try {
// First, set the URL to connect to
String url = "https://mywalmart.com/cleartrust/ct_logon_en.html";
// Next set the character encoding
String charset = "UTF-8";
// Format the query string
String query = (new String()).format ("auth_mode=%s&user=%s&password=%s&x=%s&y=%s",
URLEncoder.encode("basic", charset),
URLEncoder.encode("...", charset),
URLEncoder.encode("...", charset),
URLEncoder.encode("111", charset),
URLEncoder.encode("36", charset));
// Open a connection to the website, set a 10 second timeout, and set it to POST
URLConnection connection = new URL(url).openConnection();
connection.setReadTimeout(10000);
connection.setDoOutput(true);
// Mimic Mozilla web browser
connection.setRequestProperty("Host", "mywalmart.com");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Referer", "https://.../cleartrust/ct_logon_en.html");
// Send the POST data to the host
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(query.getBytes(charset));
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
}
// Get the headers sent to us, and display them all.
Map<String, List<String>> headers = connection.getHeaderFields ();
for (Map.Entry<String, List<String>> entry : headers.entrySet ())
{
String key = entry.getKey ();
for (String value : entry.getValue ())
System.out.println (key + ": " + value);
}
// Get the input stream for the HTML portion
InputStream response = connection.getInputStream();
Scanner in = new Scanner (response);
// Display all of the HTML
while (in.hasNextLine()) {
System.out.println (in.nextLine ());
}
} catch (IOException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
我试图模仿连接的网站上的表单是这样的(不包括一些无用的&lt; div&gt;标签等):
<form name="ctlogonform" action="ct_logon_en.html" method="post" accept-charset="UTF-8">
<input type="hidden" name="auth_mode" value="basic" />
<input type="text" name="user" />
<input type="password" name="password" />
<input type="image" src="images/btnLogin.jpg" />
</form>
现在,我运行程序时返回的所有内容都是:
null: HTTP/1.1 200 OK
Content-Length: 8069
Content-Type: text/html
后跟网页的标准HTML代码。
然后我创建了一个测试PHP网页,其代码如下:
<html>
<head>
<title>POST Test</title>
</head>
<body>
All header data:<br>
<?php
foreach (getallheaders() as $name => $value) {
echo "$name: $value<br>\n";
}
?><br>
All variables set via POST are here:<br>
<?php
foreach($_POST as $vblname => $value) echo $vblname . ' = ' . $value . "<br>\n";
?>
</body>
</html>
而且,如果我运行与上面完全相同的脚本,除了创建的PHP页面,我得到以下内容:
null: HTTP/1.1 200 OK
Date: Mon, 05 Dec 2011 02:36:48 GMT
Content-Length: 1268
Connection: close
Content-Type: text/html
Server: Apache
X-Powered-By: PHP/5.2.17
<html>
<head>
<title>POST Test</title>
</head>
<body>
All header data:<br>
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0<br>
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8<br>
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7<br>
Accept-Encoding: gzip, deflate<br>
Referer: https://mywalmart.com/cleartrust/ct_logon_en.html<br>
Host: bf-test.horizon-host.com<br>
Connection: keep-alive<br>
Content-type: application/x-www-form-urlencoded<br>
Content-Length: 60<br>
<br>
All variables set via POST are here:<br>
auth_mode = basic<br>
user = ...<br>
password = ...<br>
x = 111<br>
y = 36<br>
</body>
</html>
所以,这告诉我,我正在成功发送POST数据,并且我正确设置我的标题,唯一的问题是我的工作使用的网站没有拿起它,或者我没有将它发送给正确的网站。我已经尝试了https://mywalmart.com/cleartrust/ct_logon_en.html和https://mywalmart.com/ct_logon_en.html,但他们都做了同样的事情,并且它也没有向我发送重定向。
所以,现在已经全部覆盖了!我的问题是,我怎样才能成功地将POST数据发送到网站以模仿网络浏览器,以便我可以访问成功登录后通常会访问的页面?
(另外,我已将'...'放在各个地方,例如我的工作网站的网址,我的用户名/密码等)。
(更新:我用“......”掩盖了'mywalmart.com',然而,我意识到通过简单的谷歌搜索'ct_logon_en.html'你就可以找到原始的网址,所以我有没有真正的理由试图隐藏它。)
答案 0 :(得分:0)
首先,我会尝试点击一个不是https的URL(可能是你控制的一个),然后使用Wireshark监视输出。从您的程序和浏览器中点击它,验证您是否正确编码了表单数据。
如果那不是问题,那我接下来会猜猜饼干,但这只是猜测。
答案 1 :(得分:0)