如何使用 java 代码登录网站?
how to login into website using java code?
我想编写用于使用 java 登录网站的代码。
这是代码:
package login;
import java.net.*;
import java.io.*;
public class ConnectToURL {
// Variables to hold the URL object and its connection to that URL.
private static URL URLObj;
private static URLConnection connect;
public static void main(String[] args) {
try {
CookieManager cManager = new CookieManager();
CookieHandler.setDefault(cManager);
// Establish a URL and open a connection to it. Set it to output mode.
URLObj = new URL("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier");
connect = URLObj.openConnection();
connect.setDoOutput(true);
}
catch (MalformedURLException ex) {
System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
System.exit(1);
}
catch (Exception ex) {
System.out.println("An exception occurred. " + ex.getMessage());
System.exit(1);
}
try {
// Create a buffered writer to the URLConnection's output stream and write our forms parameters.
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
writer.write("Email=myemail@gmail.Com&Passwd=123456&submit=Login");
writer.close();
// Now establish a buffered reader to read the URLConnection's input stream.
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String lineRead = "";
// Read all available lines of data from the URL and print them to screen.
while ((lineRead = reader.readLine()) != null) {
System.out.println(lineRead);
}
reader.close();
}
catch (Exception ex) {
System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());
}
}
}
我已经在 Facebook 和 Gmail 上尝试过此代码,但问题是它不起作用。
它一直告诉我 cookie 未启用。 (我使用了 chrome 浏览器并且它们已启用)。
还有其他方法吗?
你到底想用这个做什么?几乎可以肯定,您最好使用诸如 Selenium 网络驱动程序之类的东西来完成浏览器自动化任务,因为您可以利用现有网络浏览器的工作来处理 cookie 之类的事情。
在这种情况下,您是在谈论您的网络浏览器说 cookie 未启用,但您实际上并没有使用网络浏览器,而是通过您的 java 应用程序发送连接。
如果您的目标只是登录某个网站,更好的解决方案是使用 Selenium Web Driver.
它有 API 用于创建现代驱动程序实例,并使用它们的 Web 元素进行操作。
代码示例:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
它还有如何管理 cookie 的解决方案 - Cookies
只需查看有关如何配置驱动程序实例和管理 Web 元素的文档,首选方法是使用 Page Object pattern。
更新:
要从没有 id
或 name
属性的网页获取位置,可以使用 xpath 表达式来完成,Firefox 扩展非常有用,例如:
并使用简明扼要Xpath functions。
例如:
<table>
<tr>
<td>
<p>some text here 1</p>
</td>
</tr>
<tr>
<td>
<p>some text here 2</p>
</td>
</tr>
<tr>
<td>
<p>some text here 3</p>
</td>
</tr>
</table>
获取文本 some text here 2
您可以使用以下 xpath:
//tr[2]/td/p
如果您知道文本是静态的,您可以使用 contains()
:
//p[contains(text(), 'some text here 2')]
要检查您的 xpath 在此页面是否唯一,最好使用控制台。
如何做在这里描述 How to verify an XPath expression
我想编写用于使用 java 登录网站的代码。
这是代码:
package login;
import java.net.*;
import java.io.*;
public class ConnectToURL {
// Variables to hold the URL object and its connection to that URL.
private static URL URLObj;
private static URLConnection connect;
public static void main(String[] args) {
try {
CookieManager cManager = new CookieManager();
CookieHandler.setDefault(cManager);
// Establish a URL and open a connection to it. Set it to output mode.
URLObj = new URL("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/#identifier");
connect = URLObj.openConnection();
connect.setDoOutput(true);
}
catch (MalformedURLException ex) {
System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
System.exit(1);
}
catch (Exception ex) {
System.out.println("An exception occurred. " + ex.getMessage());
System.exit(1);
}
try {
// Create a buffered writer to the URLConnection's output stream and write our forms parameters.
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
writer.write("Email=myemail@gmail.Com&Passwd=123456&submit=Login");
writer.close();
// Now establish a buffered reader to read the URLConnection's input stream.
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String lineRead = "";
// Read all available lines of data from the URL and print them to screen.
while ((lineRead = reader.readLine()) != null) {
System.out.println(lineRead);
}
reader.close();
}
catch (Exception ex) {
System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());
}
}
}
我已经在 Facebook 和 Gmail 上尝试过此代码,但问题是它不起作用。
它一直告诉我 cookie 未启用。 (我使用了 chrome 浏览器并且它们已启用)。
还有其他方法吗?
你到底想用这个做什么?几乎可以肯定,您最好使用诸如 Selenium 网络驱动程序之类的东西来完成浏览器自动化任务,因为您可以利用现有网络浏览器的工作来处理 cookie 之类的事情。
在这种情况下,您是在谈论您的网络浏览器说 cookie 未启用,但您实际上并没有使用网络浏览器,而是通过您的 java 应用程序发送连接。
如果您的目标只是登录某个网站,更好的解决方案是使用 Selenium Web Driver.
它有 API 用于创建现代驱动程序实例,并使用它们的 Web 元素进行操作。
代码示例:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
它还有如何管理 cookie 的解决方案 - Cookies
只需查看有关如何配置驱动程序实例和管理 Web 元素的文档,首选方法是使用 Page Object pattern。
更新:
要从没有 id
或 name
属性的网页获取位置,可以使用 xpath 表达式来完成,Firefox 扩展非常有用,例如:
并使用简明扼要Xpath functions。
例如:
<table>
<tr>
<td>
<p>some text here 1</p>
</td>
</tr>
<tr>
<td>
<p>some text here 2</p>
</td>
</tr>
<tr>
<td>
<p>some text here 3</p>
</td>
</tr>
</table>
获取文本 some text here 2
您可以使用以下 xpath:
//tr[2]/td/p
如果您知道文本是静态的,您可以使用 contains()
:
//p[contains(text(), 'some text here 2')]
要检查您的 xpath 在此页面是否唯一,最好使用控制台。
如何做在这里描述 How to verify an XPath expression