网络编程与通信原理有哪些核心概念?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1388个文字,预计阅读时间需要6分钟。
这个概念与研发有点脱节;一、基础概念;不同设备之间通过网络进行数据传输,并基于通用网络协议作为多种设备的兼容标准,称为网络通信。以C/S架构来看,在一次请求中,...
总感觉这个概念,和研发有点脱节;
一、基础概念
不同设备之间通过网络进行数据传输,并且基于通用的网络协议作为多种设备的兼容标准,称为网络通信;
以C/S架构来看,在一次请求当中,客户端和服务端进行数据传输的交互时,在不同阶段和层次中需要遵守的网络通信协议也不一样;
应用层:HTTP超文本传输协议,基于TCP/IP通信协议来传递数据;
传输层:TCP传输控制协议,采用三次握手的方式建立连接,形成数据传输通道;
网络层:IP协议,作用是把各种传输的数据包发送给请求的接收方;
通信双方进行交互时,发送方数据在各层传输时,每通过一层就会添加该层的首部信息;接收方与之相反,每通过一次就会删除该层的首部信息;
二、JDK源码
在java.net源码包中,提供了与网络编程相关的基础API;
1、InetAddress
封装了对IP地址的相关操作,在使用该API之前可以先查看本机的hosts的映射,Linux系统中在/etc/hosts路径下;
import java.net.InetAddress; public class TestInet { public static void main(String[] args) throws Exception { // 获取本机 InetAddress 对象 InetAddress localHost = InetAddress.getLocalHost(); printInetAddress(localHost); // 获取指定域名 InetAddress 对象 InetAddress inetAddress = InetAddress.getByName("www.baidu.com"); printInetAddress(inetAddress); // 获取本机配置 InetAddress 对象 InetAddress confAddress = InetAddress.getByName("nacos-service"); printInetAddress(confAddress); } public static void printInetAddress (InetAddress inetAddress){ System.out.println("InetAddress:"+inetAddress); System.out.println("主机名:"+inetAddress.getHostName()); System.out.println("IP地址:"+inetAddress.getHostAddress()); } }2、URL
统一资源定位符,URL一般包括:协议、主机名、端口、路径、查询参数、锚点等,路径+查询参数,也被称为文件;
3、HttpURLConnection
作为URLConnection的抽象子类,用来处理针对Http协议的请求,可以设置连接超时、读取超时、以及请求的其他属性,是服务间通信的常用方式;
public class TestHttp { public static void main(String[] args) throws Exception { // 访问 网址 内容 URL url = new URL("www.jd.com"); HttpURLConnection localhost:8082/info/99"); HttpURLConnection apiConnection = (HttpURLConnection) api.openConnection(); apiConnection.setRequestMethod("GET"); apiConnection.setConnectTimeout(3000); printHttp(apiConnection); } private static void printHttp (HttpURLConnection localhost:8083" ; public static void main(String[] args) { BasicHeader header = new BasicHeader("Token","ApacheSup") ; // 1、发送Get请求 Map<String,String> param = new HashMap<>() ; param.put("name","cicada") ; Rep getRep = doGet(BASE_URL+"/getApi_v2/3",header,param, Rep.class); System.out.println("get:"+getRep); // 2、发送Post请求 IdKey postBody = new IdKey(1,"id-key-我") ; Rep postRep = doPost (BASE_URL+"/postApi", header, postBody, Rep.class); System.out.println("post:"+postRep); } /** * 构建HttpClient对象 */ private static CloseableHttpClient buildHttpClient (){ // 请求配置 RequestConfig reqConfig = RequestConfig.custom().setConnectTimeout(6000).build(); return HttpClients.custom() .setDefaultRequestConfig(reqConfig).build(); } /** * 执行Get请求 */ public static <T> T doGet (String url, Header header, Map<String,String> param, Class<T> repClass) { // 创建Get请求 CloseableHttpClient localhost:8083" ; public static void main(String[] args) { Headers headers = new Headers.Builder().add("Token","OkHttpSup").build() ; // 1、发送Get请求 Rep getRep = execute(BASE_URL+"/getApi/1", Method.GET.name(), headers, null, Rep.class); System.out.println("get:"+getRep); // 2、发送Post请求 IdKey postBody = new IdKey(1,"id-key") ; Rep postRep = execute(BASE_URL+"/postApi", Method.POST.name(), headers, buildBody(postBody), Rep.class); System.out.println("post:"+postRep); // 3、发送Put请求 IdKey putBody = new IdKey(2,"key-id") ; Rep putRep = execute(BASE_URL+"/putApi", Method.PUT.name(), headers, buildBody(putBody), Rep.class); System.out.println("put:"+putRep); // 4、发送Delete请求 Rep delRep = execute(BASE_URL+"/delApi/2", Method.DELETE.name(), headers, null, Rep.class); System.out.println("del:"+delRep); } /** * 构建JSON请求体 */ public static RequestBody buildBody (Object body){ MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); return RequestBody.create(mediaType, JSONUtil.toJsonStr(body)) ; } /** * 构建OkHttpClient对象 */ public static OkHttpClient buildOkHttp () { return new OkHttpClient.Builder() .readTimeout(10, TimeUnit.SECONDS).connectTimeout(6, TimeUnit.SECONDS) .connectionPool(new ConnectionPool(15, 5, TimeUnit.SECONDS)) .build(); } /** * 执行请求 */ public static <T> T execute (String url, String method, Headers headers, RequestBody body, Class<T> repClass) { // 请求创建 OkHttpClient localhost:8083" ; public static void main(String[] args) { RestTemplate restTemplate = buildRestTemplate() ; // 1、发送Get请求 Map<String,String> paramMap = new HashMap<>() ; Rep getRep = restTemplate.getForObject(BASE_URL+"/getApi/1",Rep.class,paramMap); System.out.println("get:"+getRep); // 2、发送Post请求 IdKey idKey = new IdKey(1,"id-key") ; Rep postRep = restTemplate.postForObject(BASE_URL+"/postApi",idKey,Rep.class); System.out.println("post:"+postRep); // 3、发送Put请求 IdKey idKey2 = new IdKey(2,"key-id") ; restTemplate.put(BASE_URL+"/putApi",idKey2,paramMap); // 4、发送Delete请求 restTemplate.delete(BASE_URL+"/delApi/2",paramMap); // 5、自定义Header请求 HttpHeaders headers = new HttpHeaders(); headers.add("Token","AdminSup"); HttpEntity<IdKey> requestEntity = new HttpEntity<>(idKey, headers); ResponseEntity<Rep> respEntity = restTemplate.exchange(BASE_URL+"/postApi", HttpMethod.POST, requestEntity, Rep.class); System.out.println("post-header:"+respEntity.getBody()); } private static RestTemplate buildRestTemplate (){ // 1、参数配置 SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(3000); factory.setConnectTimeout(6000); // 2、创建对象 return new RestTemplate(factory) ; } }五、参考源码
编程文档: gitee.com/cicadasmile/butte-java-note 应用仓库: gitee.com/cicadasmile/butte-flyer-parent本文共计1388个文字,预计阅读时间需要6分钟。
这个概念与研发有点脱节;一、基础概念;不同设备之间通过网络进行数据传输,并基于通用网络协议作为多种设备的兼容标准,称为网络通信。以C/S架构来看,在一次请求中,...
总感觉这个概念,和研发有点脱节;
一、基础概念
不同设备之间通过网络进行数据传输,并且基于通用的网络协议作为多种设备的兼容标准,称为网络通信;
以C/S架构来看,在一次请求当中,客户端和服务端进行数据传输的交互时,在不同阶段和层次中需要遵守的网络通信协议也不一样;
应用层:HTTP超文本传输协议,基于TCP/IP通信协议来传递数据;
传输层:TCP传输控制协议,采用三次握手的方式建立连接,形成数据传输通道;
网络层:IP协议,作用是把各种传输的数据包发送给请求的接收方;
通信双方进行交互时,发送方数据在各层传输时,每通过一层就会添加该层的首部信息;接收方与之相反,每通过一次就会删除该层的首部信息;
二、JDK源码
在java.net源码包中,提供了与网络编程相关的基础API;
1、InetAddress
封装了对IP地址的相关操作,在使用该API之前可以先查看本机的hosts的映射,Linux系统中在/etc/hosts路径下;
import java.net.InetAddress; public class TestInet { public static void main(String[] args) throws Exception { // 获取本机 InetAddress 对象 InetAddress localHost = InetAddress.getLocalHost(); printInetAddress(localHost); // 获取指定域名 InetAddress 对象 InetAddress inetAddress = InetAddress.getByName("www.baidu.com"); printInetAddress(inetAddress); // 获取本机配置 InetAddress 对象 InetAddress confAddress = InetAddress.getByName("nacos-service"); printInetAddress(confAddress); } public static void printInetAddress (InetAddress inetAddress){ System.out.println("InetAddress:"+inetAddress); System.out.println("主机名:"+inetAddress.getHostName()); System.out.println("IP地址:"+inetAddress.getHostAddress()); } }2、URL
统一资源定位符,URL一般包括:协议、主机名、端口、路径、查询参数、锚点等,路径+查询参数,也被称为文件;
3、HttpURLConnection
作为URLConnection的抽象子类,用来处理针对Http协议的请求,可以设置连接超时、读取超时、以及请求的其他属性,是服务间通信的常用方式;
public class TestHttp { public static void main(String[] args) throws Exception { // 访问 网址 内容 URL url = new URL("www.jd.com"); HttpURLConnection localhost:8082/info/99"); HttpURLConnection apiConnection = (HttpURLConnection) api.openConnection(); apiConnection.setRequestMethod("GET"); apiConnection.setConnectTimeout(3000); printHttp(apiConnection); } private static void printHttp (HttpURLConnection localhost:8083" ; public static void main(String[] args) { BasicHeader header = new BasicHeader("Token","ApacheSup") ; // 1、发送Get请求 Map<String,String> param = new HashMap<>() ; param.put("name","cicada") ; Rep getRep = doGet(BASE_URL+"/getApi_v2/3",header,param, Rep.class); System.out.println("get:"+getRep); // 2、发送Post请求 IdKey postBody = new IdKey(1,"id-key-我") ; Rep postRep = doPost (BASE_URL+"/postApi", header, postBody, Rep.class); System.out.println("post:"+postRep); } /** * 构建HttpClient对象 */ private static CloseableHttpClient buildHttpClient (){ // 请求配置 RequestConfig reqConfig = RequestConfig.custom().setConnectTimeout(6000).build(); return HttpClients.custom() .setDefaultRequestConfig(reqConfig).build(); } /** * 执行Get请求 */ public static <T> T doGet (String url, Header header, Map<String,String> param, Class<T> repClass) { // 创建Get请求 CloseableHttpClient localhost:8083" ; public static void main(String[] args) { Headers headers = new Headers.Builder().add("Token","OkHttpSup").build() ; // 1、发送Get请求 Rep getRep = execute(BASE_URL+"/getApi/1", Method.GET.name(), headers, null, Rep.class); System.out.println("get:"+getRep); // 2、发送Post请求 IdKey postBody = new IdKey(1,"id-key") ; Rep postRep = execute(BASE_URL+"/postApi", Method.POST.name(), headers, buildBody(postBody), Rep.class); System.out.println("post:"+postRep); // 3、发送Put请求 IdKey putBody = new IdKey(2,"key-id") ; Rep putRep = execute(BASE_URL+"/putApi", Method.PUT.name(), headers, buildBody(putBody), Rep.class); System.out.println("put:"+putRep); // 4、发送Delete请求 Rep delRep = execute(BASE_URL+"/delApi/2", Method.DELETE.name(), headers, null, Rep.class); System.out.println("del:"+delRep); } /** * 构建JSON请求体 */ public static RequestBody buildBody (Object body){ MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); return RequestBody.create(mediaType, JSONUtil.toJsonStr(body)) ; } /** * 构建OkHttpClient对象 */ public static OkHttpClient buildOkHttp () { return new OkHttpClient.Builder() .readTimeout(10, TimeUnit.SECONDS).connectTimeout(6, TimeUnit.SECONDS) .connectionPool(new ConnectionPool(15, 5, TimeUnit.SECONDS)) .build(); } /** * 执行请求 */ public static <T> T execute (String url, String method, Headers headers, RequestBody body, Class<T> repClass) { // 请求创建 OkHttpClient localhost:8083" ; public static void main(String[] args) { RestTemplate restTemplate = buildRestTemplate() ; // 1、发送Get请求 Map<String,String> paramMap = new HashMap<>() ; Rep getRep = restTemplate.getForObject(BASE_URL+"/getApi/1",Rep.class,paramMap); System.out.println("get:"+getRep); // 2、发送Post请求 IdKey idKey = new IdKey(1,"id-key") ; Rep postRep = restTemplate.postForObject(BASE_URL+"/postApi",idKey,Rep.class); System.out.println("post:"+postRep); // 3、发送Put请求 IdKey idKey2 = new IdKey(2,"key-id") ; restTemplate.put(BASE_URL+"/putApi",idKey2,paramMap); // 4、发送Delete请求 restTemplate.delete(BASE_URL+"/delApi/2",paramMap); // 5、自定义Header请求 HttpHeaders headers = new HttpHeaders(); headers.add("Token","AdminSup"); HttpEntity<IdKey> requestEntity = new HttpEntity<>(idKey, headers); ResponseEntity<Rep> respEntity = restTemplate.exchange(BASE_URL+"/postApi", HttpMethod.POST, requestEntity, Rep.class); System.out.println("post-header:"+respEntity.getBody()); } private static RestTemplate buildRestTemplate (){ // 1、参数配置 SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(3000); factory.setConnectTimeout(6000); // 2、创建对象 return new RestTemplate(factory) ; } }
