好吧,我工作的地方是在网上发布每周的工作计划,基本上,我想编写一个程序(我最终将变成一个Android应用程序,所以我正在编写程序在Java中)发送数据到网站(我的用户名和密码),然后一旦登录,就从网站上获取时间表。一旦我抓住了时间表,我将解析它的事件(我打算将事件自动添加到手机日历中)。用Java发送POST数据
无论如何,我这样做有点麻烦。所以,基本上,我做了一个小的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);
}
}
的网站,我试图模仿一个连接上的形式是这样的(不包括一些没用<DIV>标签和等):
<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
其次standa该网页的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数据发送到网站以模仿Web浏览器,以便我可以访问成功登录后通常会访问的页面?
(更新(也,我已经在不同的地方,比如我的工作网站的网址,我的用户名/密码等地方把“...”):我曾蒙面“mywalmart .com'与'...',但是,我意识到只要通过一个简单的Google搜索'ct_logon_en.html'就可以找到原始URL,所以我没有任何真正的理由试图隐藏它。)
谢谢链接看起来很有希望,我会读它,看看我能得到这个工作。 – Alex