or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

additional-utilities.mdbean-object-manipulation.mdcollection-utilities.mdcore-string-operations.mdcryptographic-operations.mddatabase-access.mddate-time-handling.mdfile-io-operations.mdhttp-client-operations.mdindex.mdjson-processing.md

http-client-operations.mddocs/

0

# HTTP Client Operations

1

2

Comprehensive HTTP client utilities through the `HttpUtil` class, providing simple methods for HTTP requests, file downloads, and server creation based on HttpURLConnection.

3

4

## Import

5

6

```java

7

import cn.hutool.http.HttpUtil;

8

import cn.hutool.http.HttpRequest;

9

import cn.hutool.http.HttpResponse;

10

import cn.hutool.http.Method;

11

```

12

13

## Simple HTTP Methods

14

15

### GET Requests

16

17

```java { .api }

18

// Simple GET request

19

public static String get(String urlString);

20

public static String get(String urlString, Charset charset);

21

22

// GET with parameters

23

public static String get(String urlString, Map<String, Object> paramMap);

24

public static String get(String urlString, Map<String, Object> paramMap, int timeout);

25

```

26

27

### POST Requests

28

29

```java { .api }

30

// Simple POST request

31

public static String post(String urlString, String body);

32

public static String post(String urlString, Map<String, Object> paramMap);

33

34

// POST with custom content type

35

public static String post(String urlString, String body, String contentType);

36

```

37

38

### Other HTTP Methods

39

40

```java { .api }

41

// PUT requests

42

public static String put(String urlString, String body);

43

public static String put(String urlString, Map<String, Object> paramMap);

44

45

// DELETE requests

46

public static String delete(String urlString);

47

public static String delete(String urlString, Map<String, Object> paramMap);

48

49

// HEAD requests

50

public static String head(String urlString);

51

52

// PATCH requests

53

public static String patch(String urlString, String body);

54

```

55

56

## Advanced HTTP Operations

57

58

### Request Execution

59

60

```java { .api }

61

// Execute custom request

62

public static HttpResponse execute(HttpRequest request);

63

64

// Execute with specific method

65

public static HttpResponse execute(Method method, String url);

66

public static HttpResponse execute(Method method, String url, Map<String, Object> paramMap);

67

```

68

69

### Request Building

70

71

```java { .api }

72

// Create request builders

73

public static HttpRequest createGet(String url);

74

public static HttpRequest createPost(String url);

75

public static HttpRequest createPut(String url);

76

public static HttpRequest createDelete(String url);

77

public static HttpRequest createPatch(String url);

78

public static HttpRequest createHead(String url);

79

public static HttpRequest createOptions(String url);

80

public static HttpRequest createTrace(String url);

81

82

// Create request with custom method

83

public static HttpRequest createRequest(Method method, String url);

84

```

85

86

**Usage Examples:**

87

88

```java

89

// Simple requests

90

String response = HttpUtil.get("https://api.example.com/users");

91

String result = HttpUtil.post("https://api.example.com/login",

92

MapUtil.of("username", "admin", "password", "123456"));

93

94

// Advanced request building

95

HttpResponse response = HttpUtil.createPost("https://api.example.com/data")

96

.header("Content-Type", "application/json")

97

.header("Authorization", "Bearer token123")

98

.body("{\"name\":\"test\",\"value\":123}")

99

.timeout(5000)

100

.execute();

101

102

String responseBody = response.body();

103

int statusCode = response.getStatus();

104

```

105

106

## File Operations

107

108

### File Downloads

109

110

```java { .api }

111

// Download file to local path

112

public static long downloadFile(String url, String destFilePath);

113

public static long downloadFile(String url, File destFile);

114

115

// Download with progress monitoring

116

public static long downloadFile(String url, File destFile, StreamProgress streamProgress);

117

public static long downloadFile(String url, File destFile, int timeout,

118

StreamProgress streamProgress);

119

```

120

121

### File Uploads

122

123

```java { .api }

124

// Upload file via POST

125

public static String post(String urlString, Map<String, Object> paramMap, File... files);

126

public static String post(String urlString, Map<String, Object> paramMap,

127

Map<String, File> fileMap);

128

```

129

130

**Usage Examples:**

131

132

```java

133

// Download file

134

long size = HttpUtil.downloadFile("https://example.com/file.zip", "local-file.zip");

135

136

// Upload file

137

Map<String, Object> params = MapUtil.of("description", "My file");

138

File file = new File("upload.txt");

139

String result = HttpUtil.post("https://api.example.com/upload", params, file);

140

141

// Multiple file upload

142

Map<String, File> fileMap = MapUtil.of(

143

"file1", new File("doc1.pdf"),

144

"file2", new File("doc2.pdf")

145

);

146

String result = HttpUtil.post("https://api.example.com/batch-upload", null, fileMap);

147

```

148

149

## URL and Query Operations

150

151

### URL Utilities

152

153

```java { .api }

154

// URL validation

155

public static boolean isHttp(String url);

156

public static boolean isHttps(String url);

157

158

// URL encoding/decoding

159

public static String encode(String url, Charset charset);

160

public static String decode(String url, Charset charset);

161

public static String encodeParams(Map<String, Object> paramMap, Charset charset);

162

```

163

164

### Query String Operations

165

166

```java { .api }

167

// Build query string from parameters

168

public static String toParams(Map<String, Object> paramMap);

169

public static String toParams(Map<String, Object> paramMap, Charset charset);

170

171

// Parse query string

172

public static Map<String, String> decodeParamMap(String query, Charset charset);

173

```

174

175

## HttpRequest Class

176

177

Fluent API for building HTTP requests:

178

179

```java { .api }

180

public class HttpRequest {

181

// Request configuration

182

public HttpRequest method(Method method);

183

public HttpRequest url(String url);

184

185

// Headers

186

public HttpRequest header(String name, String value);

187

public HttpRequest headerMap(Map<String, String> headerMap, boolean isOverride);

188

public HttpRequest contentType(String contentType);

189

public HttpRequest userAgent(String userAgent);

190

191

// Authentication

192

public HttpRequest basicAuth(String username, String password);

193

public HttpRequest bearerAuth(String token);

194

195

// Request body

196

public HttpRequest body(String body);

197

public HttpRequest body(byte[] bodyBytes);

198

public HttpRequest form(Map<String, Object> formMap);

199

public HttpRequest form(String name, Object value);

200

201

// Parameters

202

public HttpRequest query(String name, Object value);

203

public HttpRequest query(Map<String, Object> queryMap);

204

205

// Timeouts and configuration

206

public HttpRequest timeout(int milliseconds);

207

public HttpRequest connectionTimeout(int milliseconds);

208

public HttpRequest readTimeout(int milliseconds);

209

210

// Cookies

211

public HttpRequest cookie(String name, String value);

212

public HttpRequest cookie(Cookie cookie);

213

214

// Redirects and SSL

215

public HttpRequest followRedirects(boolean followRedirects);

216

public HttpRequest disableSslValidation();

217

218

// Execution

219

public HttpResponse execute();

220

public String executeAsync();

221

}

222

```

223

224

## HttpResponse Class

225

226

Response handling and data extraction:

227

228

```java { .api }

229

public class HttpResponse {

230

// Status

231

public int getStatus();

232

public boolean isOk();

233

public boolean isRedirected();

234

235

// Headers

236

public String header(String name);

237

public Map<String, List<String>> headers();

238

public String contentType();

239

public Charset charset();

240

public long contentLength();

241

242

// Body content

243

public String body();

244

public String body(Charset charset);

245

public byte[] bodyBytes();

246

public InputStream bodyStream();

247

248

// Cookies

249

public List<HttpCookie> getCookies();

250

public String getCookie(String name);

251

252

// Utilities

253

public File writeBodyToFile(File targetFile);

254

public void close();

255

}

256

```

257

258

**Usage Examples:**

259

260

```java

261

// Complex request with authentication and custom headers

262

HttpResponse response = HttpUtil.createPost("https://api.example.com/secure-endpoint")

263

.bearerAuth("your-jwt-token")

264

.header("X-API-Version", "v2")

265

.contentType("application/json")

266

.body("{\"action\":\"process\",\"data\":[1,2,3]}")

267

.timeout(10000)

268

.execute();

269

270

// Handle response

271

if (response.isOk()) {

272

String result = response.body();

273

String contentType = response.contentType();

274

System.out.println("Success: " + result);

275

} else {

276

System.err.println("Error: " + response.getStatus());

277

}

278

```

279

280

## Cookie Management

281

282

### Global Cookie Manager

283

284

```java { .api }

285

// Enable global cookie management

286

public static void setGlobalCookieManager(CookieManager cookieManager);

287

public static CookieManager getGlobalCookieManager();

288

```

289

290

## HTTP Server

291

292

### Simple HTTP Server

293

294

```java { .api }

295

// Create simple HTTP server

296

public static SimpleServer createServer(int port);

297

public static SimpleServer createServer(int port, String root);

298

```

299

300

**Usage Example:**

301

302

```java

303

// Create and start simple HTTP server

304

SimpleServer server = HttpUtil.createServer(8080, "/var/www/html");

305

server.start();

306

```

307

308

## Constants and Enums

309

310

### HTTP Methods

311

312

```java { .api }

313

public enum Method {

314

GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, CONNECT, PATCH

315

}

316

```

317

318

### Content Types

319

320

```java { .api }

321

// Common content types

322

public static final String CONTENT_TYPE_JSON = "application/json";

323

public static final String CONTENT_TYPE_XML = "application/xml";

324

public static final String CONTENT_TYPE_FORM_URLENCODED = "application/x-www-form-urlencoded";

325

public static final String CONTENT_TYPE_MULTIPART = "multipart/form-data";

326

public static final String CONTENT_TYPE_TEXT = "text/plain";

327

public static final String CONTENT_TYPE_HTML = "text/html";

328

```

329

330

### HTTP Status Codes

331

332

Common status codes are available as constants in the response handling. The HTTP utilities automatically handle redirects, cookies, and common error conditions, making it easy to work with REST APIs and web services without external dependencies.