Skip to content

Commit d3bbe67

Browse files
author
wlanboy
committed
Added IP Resolve of DNS
1 parent 9076e15 commit d3bbe67

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/main/java/com/wlanboy/javahttpclient/client/ClientService.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.PrintWriter;
44
import java.io.StringWriter;
5+
import java.net.InetAddress;
56
import java.net.URI;
67
import java.net.http.HttpClient;
78
import java.net.http.HttpClient.Redirect;
@@ -50,6 +51,9 @@ public ResponseEntity<String> sendRequest(JavaHttpRequest requestData, HttpHeade
5051
String currentMethod = requestData.method().name();
5152
String currentBody = requestData.body();
5253

54+
// DNS-Auflösung vor dem Request (diagnostisch)
55+
String resolvedIps = resolveDns(currentUri.getHost());
56+
5357
// Erster Request mit allen Headern
5458
HttpResponse<String> response = client.send(
5559
buildRequest(currentUri, currentMethod, currentBody, requestData, incomingHeaders),
@@ -90,6 +94,7 @@ public ResponseEntity<String> sendRequest(JavaHttpRequest requestData, HttpHeade
9094
.headers(h -> {
9195
h.addAll(responseHeaders);
9296
h.set("X-Protocol-Version", protocolVersion);
97+
if (resolvedIps != null) h.set("X-Resolved-IP", resolvedIps);
9398
if (!redirectChain.isEmpty()) {
9499
h.set("X-Redirect-Chain", serializeChain(redirectChain));
95100
}
@@ -153,6 +158,19 @@ private HttpRequest buildRequest(URI uri, String method, String body,
153158
return builder.build();
154159
}
155160

161+
private String resolveDns(String hostname) {
162+
if (hostname == null || hostname.isBlank()) return null;
163+
try {
164+
InetAddress[] addresses = InetAddress.getAllByName(hostname);
165+
return java.util.Arrays.stream(addresses)
166+
.map(InetAddress::getHostAddress)
167+
.distinct()
168+
.collect(java.util.stream.Collectors.joining(", "));
169+
} catch (Exception e) {
170+
return null;
171+
}
172+
}
173+
156174
private String serializeChain(List<Map<String, Object>> chain) {
157175
StringBuilder sb = new StringBuilder("[");
158176
for (int i = 0; i < chain.size(); i++) {

src/main/resources/public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<div>
9090
<span id="statusBadge" class="badge fs-6 shadow-sm"></span>
9191
<span id="protocolBadge" class="badge bg-secondary ms-2 shadow-sm" style="display:none;"></span>
92+
<span id="resolvedIpBadge" class="badge bg-dark ms-2 shadow-sm font-monospace" style="display:none;" title="Aufgelöste IP(s)"></span>
9293
<span id="responseTime" class="ms-3 text-muted small fw-bold"></span>
9394
</div>
9495
</div>

src/main/resources/public/js/http-client.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ document.addEventListener('DOMContentLoaded', () => {
7878
const duration = Date.now() - startTime;
7979
const data = await response.text();
8080
const protocol = response.headers.get('x-protocol-version') ?? '';
81+
const resolvedIp = response.headers.get('x-resolved-ip') ?? '';
8182
const redirectChainHeader = response.headers.get('x-redirect-chain');
8283

83-
updateResponseMetadata(response.status, duration, protocol);
84+
updateResponseMetadata(response.status, duration, protocol, resolvedIp);
8485
renderRedirectChain(redirectChainHeader);
8586
addToHistory(payload, response.status, duration, data);
8687

@@ -163,7 +164,7 @@ document.addEventListener('DOMContentLoaded', () => {
163164
redirectChainDiv.style.display = 'block';
164165
}
165166

166-
function updateResponseMetadata(status, duration, protocol = '') {
167+
function updateResponseMetadata(status, duration, protocol = '', resolvedIp = '') {
167168
statusBadge.innerText = `HTTP ${status}`;
168169
statusBadge.className = `badge p-2 ${status >= 200 && status < 300 ? 'bg-success' : 'bg-danger'}`;
169170
responseTimeText.innerText = `Dauer: ${duration}ms`;
@@ -172,6 +173,15 @@ document.addEventListener('DOMContentLoaded', () => {
172173
protocolBadge.className = `badge ms-2 shadow-sm ${protocol === 'HTTP/2' ? 'bg-primary' : 'bg-secondary'}`;
173174
protocolBadge.style.display = '';
174175
}
176+
const ipBadge = document.getElementById('resolvedIpBadge');
177+
if (ipBadge) {
178+
if (resolvedIp) {
179+
ipBadge.innerText = resolvedIp;
180+
ipBadge.style.display = '';
181+
} else {
182+
ipBadge.style.display = 'none';
183+
}
184+
}
175185
}
176186

177187
function handleDetailedError(data) {

0 commit comments

Comments
 (0)