좋아요, 그래서 제가 근무하는 장소는 주간 작업 일정을 온라인으로하고 기본적으로 프로그램을 작성하고 싶습니다. (결국 Android 앱으로 바뀌므로 프로그램을 작성하고 있습니다. Java에서) 데이터를 웹 사이트 (내 사용자 이름 및 비밀번호)로 보내면 로그인 한 후 웹 사이트에서 일정을 가져옵니다. 일정을 잡으면 이벤트 (자동으로 일정으로 휴대폰 캘린더에 추가 할 예정)에 대해 구문을 분석합니다.Java로 POST 데이터 보내기
어쨌든이 작업을 수행하는 데 약간의 문제가 있습니다. 그래서, 기본적으로, 나는 웹 사이트에 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
rd 웹 페이지의 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 데이터를 어떻게 성공적으로 보낼 수 있습니까?
은 (또한, 내가 넣어 가지고 '...'등의 내 작품의 웹 사이트의 URL, 사용자 이름/암호 등 다양한 장소, 장소에)
(업데이트 : 마스크했다 'mywalmart .com 'with'... '과 같이 간단하게'ct_logon_en.html '을 검색하면 원래 URL을 찾을 수 있으므로 숨길 이유가 없습니다.
감사합니다. 링크가 유망 해 보입니다.이 링크를 읽고 작동하도록 할 수 있는지 확인합니다. – Alex