我想用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 protected]&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 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 [包含(text(),'some text here 2')]
要检查您的xpath在此页面中是否唯一,最好使用控制台。 这里介绍How to verify an XPath expression怎么做
你到底想要做什么?使用Selenium web-driver等浏览器自动化任务时,您几乎肯定会更好,因为您可以依靠现有Web浏览器的工作来处理cookie等事务。
在这种情况下,您谈论的是您的网络浏览器,说明没有启用Cookie,但您实际上并没有使用网络浏览器,而是通过您的Java应用程序发送连接。