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();
![](/assets/blank.gif)
asp.net httpclient post 请求头_Java11的HttpClient的使用相关推荐
- 给HttpClient添加请求头(HttpClientFactory)
前言 在微服务的大环境下,会出现这个服务调用这个接口,那个接口的情况.假设出了问题,需要排查的时候,我们要怎么关联不同服务之间的调用情况呢?换句话就是说,这个请求的结果不对,看看是那里出了问题. 最简 ...
- 实战HttpClient 接口调用以及获取token 设置请求头
简介: HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java.net 包中已 ...
- HttpClient的连接池||HttpClient的请求参数
连接池 如果每次请求都要创建HttpClient,会有频繁创建和销毁的问题,可以使用连接池来解决这个问题 HttpClientPoolTest.java package cn.itcast.crawl ...
- HttpClient ||GET请求||带参数的GET请求
HttpClient 网络爬虫就是用程序帮助我们访问网络上的资源,我们一直以来都是使用HTTP协议访问互联网的网页,网络爬虫需要编写程序,在这里使用同样的HTTP协议访问网页. 这里使用Java的HT ...
- HttpClient设置HTTP请求头Header
用Firebug对POST的数据进行监控 请求 HTTP头 信息,得到如下内容: Java代码 Accept application/json, text/javascript, */* Acc ...
- java 爬虫身份设定_Java爬虫中HttpClient请求头的设置
在做爬虫的时候发现如果不设置请求头的话,每次httpclient发起的请求都是响应移动端板式的,无法抓取响应的内容,后面查了一下才知道,需要重新设置请求头,让服务端误以为是某个真实浏览器发起的请求: ...
- ASP.NET Core 自定义认证方式--请求头认证
Intro 最近开始真正的实践了一些网关的东西,最近写几篇文章分享一下我的实践以及遇到的问题. 本文主要介绍网关后面的服务如何进行认证. 解决思路 网关可以做一部分的认证和授权,服务内部有时候也会需要 ...
- http使用post上传文件时,请求头和主体信息总结
请求头必须配置如下行: Content-Type' : 'multipart/form-data; boundary=---12321 boundary=---12321位文件的分界线 body如下 ...
- java http head 请求_http请求头header、请求体body、请求行介绍
HttpServletRequest对象代表客户端的请求,当客户端通过http协议请求访问 服务器的时候,http请求头的所有信息都封装在这个对象中,通过这个对象,可以获取客户端请求的所有信息.htt ...
最新文章
- fetch-pack: unexpected disconnect while reading sideband packet fatal: early EOF fatal: fetch-pack:
- 他开发了 redux,昨晚“字节一面”却挂了?
- Linux安装python3.6
- python读取excel数据并饼图_python生成excel表格以及饼图 示例源码
- Shell中自定义函数
- git-svn — 让git和svn协同工作
- python 碎片整理 threading模块小计
- python RandomTrees特征编码
- 把你的火狐浏览器改成Chrome样式
- vue2.0项目实战(3)使用axios发送请求
- hdu4707 Pet(bfs dfs,vector)
- Flex布局实现筛子3
- [原译]Lambda高手之路第二部分
- cmd进入mysql界面的命令
- LINUX编译x265:make-Makefiles.bash: ccmake: not found
- springboot 整合Oracle
- Linux虚拟网络设备之bridge(桥)
- sequence和sequencer — UVM
- java里异常名词解释_java名词解释
- 中国人保为易集康健康科技承保产品责任险,为消费者保驾护航!