日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net httpclient post 请求头_Java11的HttpClient的使用

發布時間:2025/3/21 asp.net 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net httpclient post 请求头_Java11的HttpClient的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

get 同步和異步請求

#同步 public void testGet() throws IOException, InterruptedException { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://www.toutiao.com")) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); }#異步 public void testAsyncGet() throws ExecutionException, InterruptedException { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://www.baidu.com")) .build(); CompletableFuture result = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body); System.out.println(result.get()); }

post

#json提交 public void testPostJsonGetJson() throws ExecutionException, InterruptedException, JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); StockDto dto = new StockDto(); dto.setName("hj"); dto.setSymbol("hj"); dto.setType(StockDto.StockType.SH); String requestBody = objectMapper .writerWithDefaultPrettyPrinter() .writeValueAsString(dto); HttpRequest request = HttpRequest.newBuilder(URI.create("http://toutiao/test/")) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .build(); CompletableFuture result = HttpClient.newHttpClient() .sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body) .thenApply(body -> { try { return objectMapper.readValue(body,StockDto.class); } catch (IOException e) { return new StockDto(); } }); }#form提

Authentication

package com.mkyong.http;import java.io.IOException;import java.net.Authenticator;import java.net.PasswordAuthentication;import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;public class Java11HttpClientExample6 { private final HttpClient httpClient = HttpClient.newBuilder() .authenticator(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "user", "password".toCharArray()); } }) .build(); public static void main(String[] args) throws IOException, InterruptedException { Java11HttpClientExample6 obj = new Java11HttpClientExample6(); obj.sendGET(); } private void sendGET() throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() .GET() .uri(URI.create("http://localhost:8080/books")) .setHeader("User-Agent", "Java 11 HttpClient Bot") // add request header .build(); HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); // print status code System.out.println(response.statusCode()); // print response body System.out.println(response.body()); }}

redirect

private final HttpClient httpClient = HttpClient.newBuilder() .followRedirects(HttpClient.Redirect.NEVER) .build();

Timeout

private final HttpClient httpClient = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(5)) .build();

Proxy

private final HttpClient httpClient = HttpClient.newBuilder() .proxy(ProxySelector.of(new InetSocketAddress("your-company-proxy.com", 8080))) .build();

總結

以上是生活随笔為你收集整理的asp.net httpclient post 请求头_Java11的HttpClient的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。