Http是什么

http是什么?基本概念
**http是超文本传输协议(hypertext transfer protocol)的简称,它建立在c/s架构的应用层协议。
**
**http、ftp、telnet等协议都是建立在tcp/ip协议基础上的,而tcp/ip协议是协议层的内容,它定义了计算机间通信的基础协议。
**
**在http协议中,客户端负责发起一个request,该request中含有请求方法、url、协议版本等信息,服务端在接受到该request后会返回一个response,该response中含有状态码、响应内容等信息,这一模型称为请求/响应模型。
**
http协议通信的核心是http报文,我们将其分为请求报文和响应报文。其中,由客户端发出的http报文称为请求报文,由服务端发出的报文称为响应报文.
请求报文:请求报文通常由浏览器来发起,当我们访问一个网页或者请求一个资源的时候都会产生请求报文
请求报文通常由http请求行、请求头、消息体(可选)三部分组成// 基本请求信息request url: https://www.baidu.com request method: get status code: 200 ok remote address: 111.206.209.78:443referrer policy: unsafe-url//请求头accept: application/json, text/javascript, q=0.01accept-encoding: gzip, deflate, braccept-language: zh-cn,zh;q=0.9cache-control: no-cacheconnection: keep-alivecookie: bidupsid=344d077fd6a3f97616dbd66a24ebdc96host: zhidao.baidu.compragma: no-cachereferer: https://zhidao.baidu.com/question/366757430246244692.htmluser-agent: mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/108.0.0.0 safari/537.36x-ik-ssl: 1x-requested-with: xmlhttprequest//请求参数...请求参数解析
**host: 请求目标的网站
**
**connection: 默认为“keep-alive“,默认支持长连接
**
**cache-control:这玩意跟缓存有关,其中no-cache表示无缓存
**
**user-agent:告诉serve 这个client的身份,一般由浏览器决定,比如:浏览器类型,版本等等
**
**accept:以及后面的accept打头的都是表明client能够接收的种类和类型
**
cookie:一般会将登录的一些信息放在cookie中
响应报文:响应报文是指在服务端接收并处理了客户端的请求信息以后,服务端发送给客户端的http报文
// 响应状态 ,200 表示成功http/1.1 200 ok/* 响应头部 */access-control-allow-headers: x-ik-ssl,x-ik-token,x-ik-utdata,x-ik-appid,x-ik-cuid,x-ik-fc,x-swan-version,x-ik-tries,x-fail,x-ik-appversion,x-ik-appname,content-type,swan_upgrade_checkconnection: keep-alivecontent-encoding: gzipcontent-length: 21content-type: text/htmldate: fri, 23 dec 2022 11:49:43 gmtnot-try: 0server: apachevary: accept-encodingwait: 2/* 响应信息 */...响应参数解析
**http/1.1 200 ok:200表示返回的状态码是正常,ok则是描述性的状态码
**
**date:表示服务器响应的时间
**
**server: 响应客户端的服务器。
**
**content-length:表示服务器返回给客户端正文的字节流长度
**
**content-type:表示正文的类型
**
content-encoding:文档类型的编码方式,服务器端采用gzip的形式进行了文档压缩
http的基本应用常见的方法有get、post 两种http请求方式
**get:最为常见的一种请示方式。当客户端从服务器读取文档或者通过一个链接来访问页面的时候,都是采用get方式来请求的
**
**post:post克服了get方式对参数长度存在限制的缺点,以键-值形式将参数封装在http请求中,所以从理论上讲它对参数长度没有限制,但是实际上各个服务器会规定对post提交数据大小进行限制;
**
注: post的安全性比get的高,因对用户来讲参数传递过程是不可见的。 类似用户登录之类的基本都是采用post形式
http中的五种响应码1xx:指示信息--表示请求已接收,继续处理2xx:成功--表示请求已被成功接收、理解、接受3xx:重定向--要完成请求必须进行更进一步的操作4xx:客户端错误--请求有语法错误或请求无法实现5xx:服务器端错误--服务器未能实现合法的请求!
编辑
httpwebrequest 简单的http请求案例get方式:
public static string doget(){ string url=www.baidu.com; //创建 httpwebrequest httpwebrequest = (httpwebrequest)httpwebrequest.create(url); //设置请求方法 httpwebrequest.method = get; //请求超时时间 httpwebrequest.timeout = 30000; //发送请求 httpwebresponse response=null; stream s = null; streamreader sread = null; string postcontent = null; try { //获得响应流 response = (httpwebresponse)httpwebrequest.getresponse(); s = response.getresponsestream(); sread = new streamreader(s); postcontent = sread.readtoend(); } catch (webexception ex) { response = ex.response as httpwebresponse; log.error(ex.message.tostring(), ex); } finally { if(sread != null) { sread.close(); } if(s != null) { s.close(); } if(response != null) { response.close(); } } return postcontent;//返回json数据}post方式:
public static string dopost(){ string strurl = wwww.baidu.com; //创建一个http请求 httpwebrequest request = (httpwebrequest)webrequest.create(strurl); //post请求方式 request.method = post; //内容类型 request.contenttype = application/json; request.accept = application/json; //将json字符串转化为字节 byte[] payload = system.text.encoding.utf8.getbytes(jsonparas); //设置请求的contentlength request.contentlength = payload.length; //发送请求,获得请求流 stream writer; try { writer = request.getrequeststream();//获取用于写入请求数据的stream对象 } catch (exception ex) { log.error(连接服务器失败! + ex.message.tostring(), ex); throw ex; } //将请求参数写入流 writer.write(payload, 0, payload.length); writer.close();//关闭请求流 httpwebresponse response = null; stream s = null; streamreader sread = null; string postcontent = null; try { //获得响应流 response = (httpwebresponse)request.getresponse(); s = response.getresponsestream(); sread = new streamreader(s); postcontent = sread.readtoend(); } catch (webexception ex) { response = ex.response as httpwebresponse; log.error(ex.message.tostring(), ex); } finally { if (sread != null) { sread.close(); } if (s != null) { s.close(); } if (response != null) { response.close(); } } return postcontent;//返回json数据}

Semtech的终极目标不只是LoRA芯片供应商,而是大生态链
可穿戴设备热力不减,迪士尼也来分羹
怎样用Photoshop在文本中显示图像
关于锐龙5000系列处理器的规格特性介绍
中国移动在持续提升贫困地区信息化服务水平上面取得了哪些进展
Http是什么
PESD5V0S1BA静电放电二极管 SOD-323 5V
禾多科技力推动自动驾驶系统持续迭代升级
中科创达发布汽车HMI工具链助力打造智能座舱新体验 峰岹科技被认定为广东省“专精特新”中小企业
海天雄电子:生物医疗电子实验分析
如何帮助零售商满足不同的商品需求
高通创锐讯推出高度整合联机解决方案
新思科技第四财季收入12.8亿美元 全年超分析师预期
区块链技术将促进保险市场的长期稳定繁荣
机器辅助写作提高行业的生产效率
苹果或将在今年三月推出全新iPad Pro
利用FPGA设计家用电器
加拿大推出已知旅客数字身份识别系统原型 测试生物识别和区块链在民航的应用
什么是LED贴片硅胶,最大限度LED硬件调光的正确姿势!
真菌毒素快速分析仪的技术参数都有哪些