- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用java登录互联网上的https站点,然后阅读一些信息。我已经用 firebug 查看了标题,但是我无法成功......
Firebug 告诉:
https://service.example.net/xxx/unternehmer/login.html?login=Anmelden&loginname=xxx&password=xxx&sessionid=&sprache=de
然后我想浏览这个网站:
https://service.example.net/xxx/unternehmer/ausgabe.html?code=PORTAL;sessionid=03112010150442
我怎样才能用java做到这一点?我已经尝试过类似的方法:
import java.net.*;
import java.io.*;
import java.security.*;
import javax.net.ssl.*;
public class HTTPSClient {
public static void main(String[] args) {
int port = 443; // default https port
String host = "service.example.net";
try {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
// enable all the suites
String[] supported = socket.getSupportedCipherSuites();
socket.setEnabledCipherSuites(supported);
Writer out = new OutputStreamWriter(socket.getOutputStream());
// https requires the full URL in the GET line
out.write("POST https://" + host + "//xxx/unternehmer/login.html?login=Anmelden&loginname=xxx&password=xxx&sessionid=&sprache=de HTTP/1.1\r\n");
out.write("Host: " + host + "\r\n");
out.write("\r\n");
out.flush();
// read response
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
// read the header
String s;
while (!(s = in.readLine()).equals("")) {
System.out.println(s);
}
System.out.println();
// read the length
String contentLength = in.readLine();
int length = Integer.MAX_VALUE;
try {
length = Integer.parseInt(contentLength.trim(), 16);
}
catch (NumberFormatException ex) {
// This server doesn't send the content-length
// in the first line of the response body
}
System.out.println(contentLength);
int c;
int i = 0;
while ((c = in.read()) != -1 && i++ < length) {
System.out.write(c);
}
System.out.println("1.part done");
out.close();
in.close();
socket.close();
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
不幸的是,这不适用于登录......我也不知道从哪里获取这个 sessionid...每次都是不同的。我希望你可以帮助我。ps:我把一些相关信息替换成了xxx
最佳答案
问题解决了:)
首先我从 apache 添加了库:
然后我结合了几个教程。
我的代码:
import java.io.BufferedWriter;
import java.io.FileWriter;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.client.params.CookiePolicy;
public class Test {
public static final String TARGET_HTTPS_SERVER = "www.example.net";
public static final int TARGET_HTTPS_PORT = 443;
public static void main(String[] args) throws Exception {
HttpClient httpClient = new HttpClient();
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
PostMethod post = new PostMethod("https://www.example.com/login.html");
post.setRequestHeader(new Header(
"User-Agent", "Mozilla/5.0 /Windows; U; Windows NT 4.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.0"));
post.addParameter("login", "true");
post.addParameter("username", "xxx");
post.addParameter("password", "xxx");
post.addParameter("language", "de");
httpClient.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
String body=post.getResponseBodyAsString();
//Get the session id by parsing the code, i know this is not pretty
String sessionid=body.substring(body.indexOf("session")+10,body.indexOf("session")+10+14);
System.out.print(sessionid);
GetMethod get=new GetMethod("https://www.example.com/thesiteyouwannabrowse?sessionid="+sessionid);
get.setRequestHeader(new Header(
"User-Agent", "Mozilla/5.0 /Windows; U; Windows NT 4.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.0"));
httpClient.executeMethod(get);
System.out.println(get.getResponseBodyAsString());
//write it into a file
try{
// Create file
FileWriter fstream = new FileWriter("file.html");
BufferedWriter out = new BufferedWriter(fstream);
out.write(get.getResponseBodyAsString());
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
post.releaseConnection();
}
}
关于java https登录和浏览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4087610/
我遇到一个问题,即我的抓取工具正在跳过没有浏览引荐来源网址的浏览页面。我正在尝试解析 URL 中包含/browse/的所有页面,无论引用者如何。 以下是我的代码(根据 paul t 更新): from
我有4个屏幕。 X,A,B,C。导航模式应如下所示 这是代码 X 屏幕 class X extends StatelessWidget { @override Widget build(Bui
我想用 java 编写一个简单的网络浏览器,这是我的代码! import javax.swing.*; import java.io.*; import java.awt.*; import java
这个问题在这里已经有了答案: A home button in iOS 5, xcode 4.2, Story board (2 个回答) 8年前关闭。 来自 FirstView到SecondView
我使用 C#/ASP.Net 在 IIS7 中创建虚拟目录,以便外部人员可以浏览各种文档。 一切看起来都不错,除了浏览是一种沉闷的文本格式。当他们浏览到文件夹时,我如何创建更多的“Windows 资源
我想我在 Chrome 或 Internet Explorer 中见过 Gmail 这样做,但我从未在 Firefox 中见过它。我想我还是会问一下。是否可以在不需要的情况下进行文件上传?我看到你可以
我是 Java 的新手,作为第一次阅读,我阅读了几本有关 Java 语言的书籍。 我有几个关于 Java 文档的问题。如何“导航”它们?是否可以仅使用 Javadoc 来学习新概念? 这是一个示例 -
我有一个解析网络,现在我想浏览标签,或显示图表。我怎样才能得到图表?或者在树中导航。显示第一步然后其他等。并了解这棵树是如何 build 的。 import urllib from lxml impo
考虑以下情况: 杀戮戒指中的 N 项。需要拉取的项目是项目#k 数值论证解决方案不会真正起作用,因为计算或跟踪杀死环中事物的位置很烦人。 最佳答案 实际问题是什么?按 C-y 然后按 M-y k 次有
这是我之前的 question 的后续。 我试图从 here 了解 Haskell 中的列表拆分示例: foldr (\a ~(x,y) -> (a:y,x)) ([],[]) 我可以阅读 Haske
是否有任何 vim 工具通过 Latex 文档结构提供有效的导航。拥有像 NERDTree 面板这样的东西来表示 latex 文档的部分/子部分结构会非常有用。 最佳答案 扩展 mnosefishs
您好,我的应用程序中有一个模态视图 Controller ,当按下某个按钮时,该 Controller 会消失,并且其中带有 UITableView 的 View 会使用导航 Controller 滑
Glances v2.11.1 with psutil v5.4.3 /usr/lib/python3.6/site-packages/psutil/_pslinux.py:1152: Runtime
我正在使用 Eclipse,我希望我可以通过按 STRG 在以驼峰式书写的单词之间跳转。现在我正在使用 Sublime,我找不到这样做的快捷方式,也找不到实现它的插件。 下面的例子说明了我的问题 aF
我的主程序提示用户浏览文件以便使用 ffmpeg 进行转换。这是文件浏览的格式: 1. Select audio file for conversion ( mp3, wma):
我正在尝试浏览 SVN 存储库,而不必检查它: 是否可以在本地(Unix 上)执行此操作? 这可以通过 ssh 访问实现吗? 最佳答案 svn ls 有效。例如 svn ls http://my.sv
我有一个网站,其中列出了数据库中的企业列表。在每个页面上,您可以执行不同的操作,例如将其转发给 friend 、打印页面等。我的问题是我可以使用谷歌分析来跟踪每个列表的展示次数和浏览次数吗?因此,如果
在 Dart 我有一个嵌套的元素列表 void main() { var diction = {'1':'Alpha','2':'Beta','3':{'x':'Football','y':'
当我们的用户单击网页上的浏览按钮来上传任何文件时,我想更改窗口位置。我会给你一个场景 - 我的 html 表单上有一个浏览按钮,当用户单击一个弹出窗口时,该位置默认为“我的文档”。 实际上我想在单击“
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我是一名优秀的程序员,十分优秀!