JavaHTTPS登录和浏览
我想用java登录到internet上的https站点,然后阅读一些信息。我已经用firebug查看了标题,但是我没能做到 Firebug告诉我们:JavaHTTPS登录和浏览,java,sockets,login,https,safe-browsing,Java,Sockets,Login,Https,Safe Browsing,我想用java登录到internet上的https站点,然后阅读一些信息。我已经用firebug查看了标题,但是我没能做到 Firebug告诉我们: https://service.example.net/xxx/unternehmer/login.html?login=Anmelden&loginname=xxx&password=xxx&sessionid=&sprache=de 然后我想浏览到这个网站: https://service.example.ne
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);
}
}
}
import java.net.*;
导入java.io.*;
导入java.security.*;
导入javax.net.ssl.*;
公共类HttpScClient{
公共静态void main(字符串[]args){
int-port=443;//默认https端口
String host=“service.example.net”;
试一试{
SSLSocketFactory=(SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket套接字=(SSLSocket)工厂.createSocket(主机,端口);
//启用所有套件
String[]supported=socket.getSupportedCipherSuite();
socket.SetEnablediPhone套件(支持);
Writer out=新的OutputStreamWriter(socket.getOutputStream());
//https需要获取行中的完整URL
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+”\r\n”);
out.write(“\r\n”);
out.flush();
//读取响应
BufferedReader in=新的BufferedReader(
新的InputStreamReader(socket.getInputStream());
//阅读标题
字符串s;
而(!(s=in.readLine()).equals(“”){
系统输出打印项次;
}
System.out.println();
//读长度
字符串contentLength=in.readLine();
int length=Integer.MAX_值;
试一试{
length=Integer.parseInt(contentLength.trim(),16);
}
捕获(NumberFormatException ex){
//此服务器不发送内容长度
//在响应主体的第一行中
}
System.out.println(contentLength);
INTC;
int i=0;
while((c=in.read())!=-1&&i++
不幸的是,这不适用于登录。。。。
我也不知道在哪里可以找到这个会话ID…每次都是不同的会话。
我希望你能帮助我。
ps:我用xxx替换了一些相关信息,我自己也做过类似的事情。我使用这种“手动”方法使它工作起来,但这相当麻烦,尤其是在cookie管理方面 我建议你去看看。(当我意识到使用这个库是多么容易时,我扔掉了我的代码。) 正如org.life.java所指出的,下面是一个关于如何使用该库开始使用SSL的好方法。问题已解决:) 首先,我添加了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();
}
}