vlang
  • 目录
    • V语言学习笔记
    • 目录
    • 安装
    • 开发工具
    • 快速总览
    • 模块
    • 基本类型
    • 变量
    • 常量
    • 枚举
    • 数组
    • 字典
    • 流程控制
    • 函数
    • 结构体
    • 访问控制
    • 方法
    • 注解
    • 接口
    • 泛型
    • 类型别名
    • 联合类型
    • 联合体
    • 错误处理
    • 运算符重载
    • 内置json支持
    • 内置sql支持
    • 并发
    • 内存管理
    • 代码测试
    • 文档生成
    • 编译时代码
    • 包管理器
    • 单个V文件
    • V shell script
    • 不安全代码
    • 集成C代码库
    • 集成汇编代码
    • 裸机环境
    • 生成wasm
    • GUI开发
    • web开发
    • 数据库开发
  • builtin
  • strings
  • arrays
  • maps
  • datatypes
  • strconv
  • os
  • runtime
  • time
  • math
  • json
  • encoding
  • compress
  • toml
  • flag
  • term
  • log
  • io
  • readline
  • reflection
  • net
  • net.http
  • eventbus
  • regex
  • crypto
  • rand
  • sync
  • x
  • db.pg
  • db.mysql
  • db.mssql
  • db.sqlite
  • orm
  • ui
  • sokol
  • gg
  • gx
  • fontstash
  • stbi
  • clipboard
  • V抽象语法树
  • V语言服务
  • V编译器源代码
  • 生成C代码
  • 生成js代码
  • 生成go代码
  • 生成native代码
  • 解释器直接运行
  • 附录1 关键字
  • 附录2 运算符
  • 附录3 编码风格
  • 附录4 V编译器命令行使用
  • 附录5 V调试及错误定位
  • 附录6 V和Go基本语法参照
  • 附录7 V和Zig基本语法参照
由 GitBook 提供支持
在本页
  • 公共枚举
  • http协议版本
  • http方法
  • http状态码
  • 公共结构体
  • http请求
  • http响应
  • header头部
  • cookie
  • http客户端
  • http服务端
  • 关键概念

这有帮助吗?

net.http

公共枚举

http协议版本

用来表示http协议版本的枚举

pub enum Version { 
	unknown
	v1_1
	v2_0
	v1_0
}

http方法

用来表示http方法的枚举

pub enum Method {
	get
	post
	put
	head
	delete
	options
	trace
	connect
	patch
}

http状态码

pub enum Status {
	unknown = -1
	unassigned = 0
	cont = 100
	switching_protocols = 101
	processing = 102
	checkpoint_draft = 103
	ok = 200
	created = 201
	accepted = 202
	non_authoritative_information = 203
	no_content = 204
	reset_content = 205
	partial_content = 206
	。。。

公共结构体

http请求

pub struct Request {
pub mut:
	version    Version = .v1_1
	method     Method
	header     Header
	cookies    map[string]string
	data       string
	url        string
	user_agent string = 'v.http'
	verbose    bool
	user_ptr   voidptr

	read_timeout  i64 = 30 * time.second
	write_timeout i64 = 30 * time.second
	
	validate               bool
	verify                 string
	cert                   string
	cert_key               string
	in_memory_verification bool 
}

构造函数:

pub fn new_request(method Method, url_ string, data string) !Request
//解析raw HTTP request,变成一个Request对象
pub fn parse_request(mut reader io.BufferedReader) !Request 

方法:

pub fn (mut req Request) add_header(key CommonHeader, val string) 
//执行请求,把请求发送到服务端,返回响应
pub fn (req &Request) do() !Response 

http响应

pub struct Response {
pub mut:
	text         string
	header       Header
	status_code  int
	status_msg   string
	http_version string
}

构造函数:

//构造参数
pub struct ResponseConfig {
	version Version = .v1_1
	status  Status  = .ok
	header  Header
	text    string
}
pub fn new_response(conf ResponseConfig) Response

方法:

pub fn (r Response) status() Status
pub fn (mut r Response) set_status(s Status)
pub fn (r Response) version() Version
pub fn (mut r Response) set_version(v Version)

header头部

[noinit]
pub struct Header {
mut:
	data map[string][]string
	keys map[string][]string
}

构造函数:

//构造参数
pub struct HeaderConfig {
	key   CommonHeader
	value string
}
pub fn new_header(kvs ...HeaderConfig) Header

方法:

pub fn (mut h Header) add(key CommonHeader, value string)
pub fn (mut h Header) set(key CommonHeader, value string)
pub fn (mut h Header) delete(key CommonHeader)
pub fn (h Header) contains(key CommonHeader) bool
pub fn (h Header) get(key CommonHeader) !string
pub fn (h Header) values(key CommonHeader) []string
pub fn (h Header) keys() []string
pub fn (h Header) join(other Header) Header
...

cookie

pub struct Cookie {
pub mut:
	name        string
	value       string
	path        string   
	domain      string   
	expires     time.Time 
	raw_expires string   

	max_age   int
	secure    bool
	http_only bool
	same_site SameSite
	raw       string
	unparsed  []string
}

http客户端

发起请求的配置:

pub struct FetchConfig {
pub mut:
	url        string
	method     Method
	header     Header
	data       string
	params     map[string]string
	cookies    map[string]string
	user_agent string = 'v.http'
	verbose    bool
	
	validate               bool   
	verify                 string 
	cert                   string 
	cert_key               string 
	in_memory_verification bool   
}

发起客户端请求函数:

pub fn fetch(config FetchConfig) !Response	//通用的发起请求函数,给以下函数统一调用

pub fn get(url string) !Response	//http.get
pub fn post(url string, data string) !Response	//http.post
pub fn put(url string, data string) !Response	//http.put
pub fn head(url string) !Response	//http.head
pub fn patch(url string, data string) !Response	//http.patch
pub fn delete(url string) !Response	//http.delete

pub fn post_json(url string, data string) !Response  //传输json数据
pub fn post_form(url string, data map[string]string) !Response	//传输表单数据
pub fn post_multipart_form(url string, conf PostMultipartFormConfig) !Response//附件

http服务端

pub struct Server {
mut:
	state    ServerStatus = .closed
	listener net.TcpListener
pub mut:
	port           int           = 8080
	handler        Handler       = DebugHandler{}
	read_timeout   time.Duration = 30 * time.second
	write_timeout  time.Duration = 30 * time.second
	accept_timeout time.Duration = 30 * time.second
}

关键概念

客户端client

服务端server

发送请求request

回复响应response

报文类型:请求报文,响应报文

http协议,http协议版本

http方法

报文结构:报文头部header,报文主体body

报文头部 header,有的报文只可用于请求,用的报文只可用于响应,有的2者都可用

状态码 status

状态码短语 status phase

主机host

URI统一资源定位

无状态协议

cookie

上一页net下一页eventbus

最后更新于1年前

这有帮助吗?