我试图在Java不使用外部库或HttpURLConnection类的情况下通过SSL Socket进行快速HTTP GET,并注意到结果因使用的HTTP服务器类型而异。
以下是主要的Java代码:
private static SSLContext context = null;
private static KeyStore keystore = null;
private static SSLSocketFactory sslClientSockFactory = null;
private static KeyManagerFactory kmFactory = null;
private static KeyManager[] keyMan = null;
private static TrustManager[] trustMan = null;
private static int portNum = 443;
private static String address = "www.google.com";
protected static final String sslContext = "TLSv1.2";
private static OutputStream sockOS = null;
private static InputStream sockIS = null;
public static void main(String[] args) {
try {
// Load HTTPS keystore from nothing
keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(null, null);
kmFactory = KeyManagerFactory.getInstance("PKIX");
kmFactory.init(keystore, null);
keyMan = kmFactory.getKeyManagers();
trustMan = new TrustManager[] { new TrustedTrustManager() };
context = SSLContext.getInstance(sslContext);
context.init(keyMan, trustMan, null);
sslClientSockFactory = context.getSocketFactory();
// Open HTTPS socket
System.out.println("Attempting to open client ssl sock: " + address + ":" + portNum);
if (sslClientSockFactory == null) {
System.out.println("[ERR] Client socket factory is NULL !!!");
}
SSLSocket socket = (SSLSocket) sslClientSockFactory.createSocket(address, portNum);
boolean isConnected = socket.isConnected();
System.out.println("Is Connected: " + isConnected);
System.out.println("Is Closed: " + socket.isClosed());
System.out.println("Is InputShutdown: " + socket.isInputShutdown());
System.out.println("Is OutputShutdown: " + socket.isOutputShutdown());
if (isConnected) {
String reqStr = "GET / HTTP/1.1\r\n"
+ "Host: www.google.com\r\n"
+ "User-Agent: curl/7.79.1\r\n"
+ "Accept: */*\r\n\r\n";
sockOS = socket.getOutputStream();
sockIS = socket.getInputStream();
sockOS.write(reqStr.getBytes());
sockOS.flush();
byte[] response = sockIS.readAllBytes();
System.out.println("Response Len: " + response.length);
socket.close();
} else {
System.out.println("[ERR] Failed to connect to destination server ...");
}
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException
| UnrecoverableKeyException | KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
对于TrustedTrustManager代码:
public class TrustedTrustManager implements X509TrustManager {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] certs,
String authType) {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] certs,
String authType) {
}
}
不知何故,我可以访问几个lighttpd托管的网页,但这并没有扩展到谷歌和许多其他类型的服务器。
成功查询lighttpd自己主页的一个示例:
Attempting to open client ssl sock: www.lighttpd.net:443
Is Connected: true
Is Closed: false
Is InputShutdown: false
Is OutputShutdown: false
Request String:
GET / HTTP/1.1
Host: www.lighttpd.net
User-Agent: curl/7.79.1
Accept: */*
Response Len: 5670
Response:
HTTP/1.1 200 OK
ETag: "3058255306"
Last-Modified: Wed, 19 Jan 2022 18:07:58 GMT
Accept-Ranges: bytes
Content-Type: text/html
Vary: Accept-Encoding
Strict-Transport-Security: max-age=31536000
Content-Length: 5387
Date: Tue, 17 May 2022 15:24:50 GMT
Server: lighttpd/2.0.0
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Home - Lighttpd - fly light</title>
<link type="application/atom+xml" href="http://www.lighttpd.net/feed/atom.xml" rel="alternate" title="Atom feed" />
<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/app.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid"><div class="row-fluid">
<div class="span2">
<div style="width: 170px; margin: 0 auto;">
<a href="/" id="logo" alt="lighttpd"></a>
<div class="well well-small">
<ul class="nav nav-list">
<li class="nav-header">
Search
<a class="pull-right feedicon" href="/feed/atom.xml">
<i class="feedicon-small"></i>
</a>
</li>
<li>
.....More info not shown here .....
</div>
</div>
</div></div>
<script src="/js/jquery-1.8.3.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>
如果您知道我的代码缺少什么,从而使它可以在lighttpd托管的服务器上工作,而不能在其他类型的服务器上工作,我们将不胜感激。
找到了答案。这是一个简单的解决方案。
谢谢你的-1。
Attempting to open client ssl sock: www.google.com:443
Is Connected: true
Is Closed: false
Is InputShutdown: false
Is OutputShutdown: false
HTTP/1.1 200 OK
Date: Tue, 17 May 2022 16:22:17 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2022-05-17-16; expires=Thu, 16-Jun-2022 16:22:17 GMT; path=/; domain=.google.com; Secure
Set-Cookie: AEC=AakniGPcJmAKB3nIVsBIA3Xg-vq9cxGQ2HOxdRvp15U76U7IfOSWP-lCRg; expires=Sun, 13-Nov-2022 16:22:17 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
qzuW9eN5utjjrbwbmGqzvjyQ1GHADHuwEkkL5tX8RKae7FNAmKXm-4QoGF9nS6wmwhGZeMq8gnVEn27pRI4-P7oAxAyg; expires=Wed, 16-Nov-2022 16:22:17 GMT; path=/; domain=.google.com; HttpOnly
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked
3a48
.....More info not shown here .....
\x22stok\x22:\x22fJdxZy6Sfc7-Pi0HckoyI_RHf40\x22,\x22uhde\x22:false}}';google.pmc=JSON.parse(pmc);})();</script> </body></html>
0
我正在尝试使用ant从现有代码到war文件生成web服务。Eclipse生成一个完整的ant构建文件(axis_bujava.xml ),它可以工作(但是undeploy.wsdd并不是一直生成的,我不知道为什么),但前提是我从eclipse运行它。 我想有一个独立的脚本来生成我的网络服务(并将其打包到warfile后,但这不是问题^^) 我从eclipse插件文件夹中添加了一些jar到类路径并创
不使用回溯方法是否可能实现DFS算法?如果是,那么请说明如何做到这一点。
文档说这个库运行在GPU上。如果我功能强大的笔记本电脑没有GPU,我还能运行Deeplearning4J吗?
我正在使用Transform创建一个windows安装包,使其成为多实例。我的mst文件更新了某些注册表项组件的产品代码和GUID。这是每台机器安装。现在我无法卸载我的产品,如果: 我的mst文件从其原始位置删除 TransformsSecure策略设置为1 安装程序尝试在原始位置查找mst文件,但无法执行此操作,卸载失败。在这两种情况下,我可以做什么让我的产品卸载? 一些额外信息。我看到我的ms
我不想再使用powermock了。因为junit5开始模拟静态类。因此,我试图摆脱powermock方法。 当我使用PowerMock时,我可以很容易地发现一个具有私有构造函数的类,然后我调用静态方法。 这是我代码的一部分(当我使用PowerMock时) 在我做了MessageValidationUtils.class的间谍对象后,我正在测试这个: 经过一些研究,我找不到任何与监视一个具有私有构造
我看到的所有解决方案都需要使用。但是,我想在Eclipse之外的单个文件上使用CDT解析器。那有什么办法吗?