好吧,我工作的地方是在網上發佈每週的工作計劃,基本上,我想編寫一個程序(我最終將變成一個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