0
# Network Interception
1
2
Network monitoring and HTTP request/response interception capabilities for the Chrome DevTools Protocol v85. This functionality allows you to monitor network traffic, modify requests and responses, handle authentication challenges, and control caching behavior.
3
4
## Capabilities
5
6
### V85Network Class
7
8
The main class for network interception extending the base Network functionality.
9
10
```java { .api }
11
/**
12
* Network monitoring and request/response interception for Chrome DevTools v85
13
*/
14
public class V85Network extends Network<AuthRequired, RequestPaused> {
15
/**
16
* Creates a new V85Network instance
17
* @param devTools - DevTools session for sending commands
18
*/
19
public V85Network(DevTools devTools);
20
}
21
```
22
23
### User Agent Management
24
25
Control the browser's user agent string and related headers.
26
27
```java { .api }
28
/**
29
* Sets user agent override for network requests
30
* @param userAgent - User agent configuration including UA string, language, and platform
31
* @return Command to set user agent override
32
*/
33
protected Command<Void> setUserAgentOverride(UserAgent userAgent);
34
```
35
36
**Usage Example:**
37
38
```java
39
import org.openqa.selenium.devtools.v85.V85Network;
40
import org.openqa.selenium.devtools.idealized.Network.UserAgent;
41
42
V85Network network = domains.network();
43
UserAgent customUA = new UserAgent("Mozilla/5.0 (Custom Browser) WebKit/537.36")
44
.acceptLanguage("en-US,en")
45
.platform("Linux x86_64");
46
devTools.send(network.setUserAgentOverride(customUA));
47
```
48
49
### Cache Control
50
51
Enable or disable network caching for the browser session.
52
53
```java { .api }
54
/**
55
* Enables network caching for the session
56
* @return Command to enable caching
57
*/
58
protected Command<Void> enableNetworkCaching();
59
60
/**
61
* Disables network caching for the session
62
* @return Command to disable caching
63
*/
64
protected Command<Void> disableNetworkCaching();
65
```
66
67
**Usage Example:**
68
69
```java
70
// Disable caching for testing
71
devTools.send(network.disableNetworkCaching());
72
73
// Re-enable caching later
74
devTools.send(network.enableNetworkCaching());
75
```
76
77
### Request Interception
78
79
Set up and manage request interception for all network patterns.
80
81
```java { .api }
82
/**
83
* Enables fetch interception for all request patterns
84
* @return Command to enable request/response interception
85
*/
86
protected Command<Void> enableFetchForAllPatterns();
87
88
/**
89
* Disables fetch interception
90
* @return Command to disable interception
91
*/
92
protected Command<Void> disableFetch();
93
94
/**
95
* Event fired when a request is paused for interception
96
* @return Event for paused requests
97
*/
98
public Event<RequestPaused> requestPausedEvent();
99
```
100
101
**Usage Example:**
102
103
```java
104
// Enable request interception
105
devTools.send(network.enableFetchForAllPatterns());
106
107
// Listen for paused requests
108
devTools.addListener(network.requestPausedEvent(), pausedRequest -> {
109
// Handle the intercepted request
110
handleRequest(pausedRequest);
111
});
112
```
113
114
### Request/Response Processing
115
116
Process intercepted requests and responses with modification capabilities.
117
118
```java { .api }
119
/**
120
* Creates HttpRequest or HttpResponse from paused request data
121
* @param pausedReq - The paused request data from CDP
122
* @return Either HttpRequest (for requests) or HttpResponse (for responses)
123
*/
124
public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedReq);
125
126
/**
127
* Checks if the paused request has an error response
128
* @param pausedReq - The paused request to check
129
* @return true if there's an error response
130
*/
131
protected boolean hasErrorResponse(RequestPaused pausedReq);
132
133
/**
134
* Gets the request ID from a paused request
135
* @param pausedReq - The paused request
136
* @return Request ID as string
137
*/
138
protected String getRequestId(RequestPaused pausedReq);
139
```
140
141
### Request Continuation
142
143
Continue requests with or without modifications.
144
145
```java { .api }
146
/**
147
* Continues a paused request without any modifications
148
* @param pausedRequest - The paused request to continue
149
* @return Command to continue request unmodified
150
*/
151
protected Command<Void> continueWithoutModification(RequestPaused pausedRequest);
152
153
/**
154
* Continues a paused request with modifications
155
* @param pausedReq - The paused request to modify
156
* @param req - Modified HttpRequest to send instead
157
* @return Command to continue with modified request
158
*/
159
protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req);
160
161
/**
162
* Fulfills a paused request with a custom response
163
* @param pausedReq - The paused request to fulfill
164
* @param res - Custom HttpResponse to return
165
* @return Command to fulfill request with custom response
166
*/
167
protected Command<Void> fulfillRequest(RequestPaused pausedReq, HttpResponse res);
168
```
169
170
**Usage Example:**
171
172
```java
173
import org.openqa.selenium.remote.http.Contents;
174
175
devTools.addListener(network.requestPausedEvent(), pausedRequest -> {
176
if (pausedRequest.getRequest().getUrl().contains("/api/data")) {
177
// Fulfill with custom response
178
HttpResponse customResponse = new HttpResponse()
179
.setStatus(200)
180
.setContent(Contents.utf8String("{\"custom\": \"data\"}"));
181
devTools.send(network.fulfillRequest(pausedRequest, customResponse));
182
} else {
183
// Continue without modification
184
devTools.send(network.continueWithoutModification(pausedRequest));
185
}
186
});
187
```
188
189
### Authentication Handling
190
191
Handle HTTP authentication challenges automatically.
192
193
```java { .api }
194
/**
195
* Event fired when authentication is required
196
* @return Event for authentication challenges
197
*/
198
protected Event<AuthRequired> authRequiredEvent();
199
200
/**
201
* Gets the URI from an authentication challenge
202
* @param authRequired - The authentication challenge
203
* @return URI that requires authentication
204
*/
205
protected String getUriFrom(AuthRequired authRequired);
206
207
/**
208
* Continues authentication with provided credentials
209
* @param authRequired - The authentication challenge
210
* @param credentials - Username and password to use
211
* @return Command to provide credentials
212
*/
213
protected Command<Void> continueWithAuth(AuthRequired authRequired, UsernameAndPassword credentials);
214
215
/**
216
* Cancels authentication challenge
217
* @param authRequired - The authentication challenge to cancel
218
* @return Command to cancel authentication
219
*/
220
protected Command<Void> cancelAuth(AuthRequired authRequired);
221
```
222
223
**Usage Example:**
224
225
```java
226
// Handle authentication challenges
227
devTools.addListener(network.authRequiredEvent(), authChallenge -> {
228
String uri = network.getUriFrom(authChallenge);
229
if (uri.contains("secure-api.example.com")) {
230
UsernameAndPassword creds = new UsernameAndPassword("username", "password");
231
devTools.send(network.continueWithAuth(authChallenge, creds));
232
} else {
233
devTools.send(network.cancelAuth(authChallenge));
234
}
235
});
236
```
237
238
## CDP Model Classes
239
240
### AuthRequired
241
242
Authentication challenge information from the browser.
243
244
```java { .api }
245
public class AuthRequired {
246
public RequestId getRequestId();
247
public AuthChallenge getAuthChallenge();
248
}
249
250
public class AuthChallenge {
251
public String getOrigin();
252
public String getScheme();
253
public String getRealm();
254
}
255
```
256
257
### RequestPaused
258
259
Information about a paused network request or response.
260
261
```java { .api }
262
public class RequestPaused {
263
public RequestId getRequestId();
264
public Request getRequest();
265
public Optional<Integer> getResponseStatusCode();
266
public Optional<List<HeaderEntry>> getResponseHeaders();
267
public Optional<String> getResponseErrorReason();
268
}
269
```
270
271
### Request
272
273
Network request details.
274
275
```java { .api }
276
public class Request {
277
public String getUrl();
278
public String getMethod();
279
public Map<String, String> getHeaders();
280
public Optional<String> getPostData();
281
}
282
```
283
284
### HeaderEntry
285
286
HTTP header name/value pair.
287
288
```java { .api }
289
public class HeaderEntry {
290
public HeaderEntry(String name, String value);
291
public String getName();
292
public String getValue();
293
}
294
```
295
296
### RequestPattern
297
298
Pattern for request interception configuration.
299
300
```java { .api }
301
public class RequestPattern {
302
public RequestPattern(Optional<String> urlPattern, Optional<String> resourceType, Optional<RequestStage> interceptionStage);
303
}
304
305
public enum RequestStage {
306
REQUEST,
307
RESPONSE
308
}
309
```