当前位置: 首页 > news >正文

制作网站的原因网站页面布局的目的

制作网站的原因,网站页面布局的目的,在泰安市有做阿里巴巴网站的,门户网站建设方案1. 定制HTTP请求 如果需要对向服务器发送的HTTP请求做更多超越于默认设置的定制化。 client : http.Client{} 使用net/http包提供的导出类型Client,创建一个表示客户端的变量。request, err : http.NewRequest("GET", "https://ifconfig.io/ip&quo…

1. 定制HTTP请求

如果需要对向服务器发送的HTTP请求做更多超越于默认设置的定制化。

  • client := http.Client{}
    • 使用net/http包提供的导出类型Client,创建一个表示客户端的变量。
  • request, err := http.NewRequest("GET", "https://ifconfig.io/ip", nil) 
    • 调用net/http包提供的导出函数NewRequest,构建一个HTTP请求。
    • 参数解析:请求类型,目标url;返回1个request变量。
  • response, err := client.Do(request)
    • 用所创建的客户端发送所构建的HTTP请求,并获取响应。

用这种方法可以单独设置请求头基本身份验证cookies等请求参数。

一般而言,除非要完成的任务非常简单,否则推荐使用这种定制化方法。

// 定制HTTP请求
// 如果快捷方法产生的简单GET请求不足以满足对请求报文
// 做进一步控制的需要,则可以使用自定义的HTTP客户端
package main
import ("fmt""io/ioutil""log""net/http"
)func main() {client := http.Client{}request, err := http.NewRequest("GET", "https://ifconfig.io/ip", nil)if err != nil {log.Fatal(err)}response, err := client.Do(request)if err != nil {log.Fatal(err)}defer response.Body.Close()resBody, err := ioutil.ReadAll(response.Body)if err != nil {log.Fatal(err)}fmt.Printf("%s", resBody)
}
// 打印输出:
xxx.xxx.xxx.xxx 显示客户端ip,已隐去

 2. 调试HTTP

Go语言标准库的net/http/httputil包提供了一些方法,可用于调试往返于客户端和服务器之间的HTTP请求响应

  • 打印请求包,下面2个函数均返回关于“请求”或“响应”的字节切片,转为为字符串格式即可打印显示。
    • debugRequest, err := httputil.DumpRequestOut(request, true)
    • fmt.Printf("%s", debugRequest)
  • 打印响应包
    • debugResponse, err := httputil.DumpResponse(response, true)
    • fmt.Printf("%s", debugResponse)

如果我们希望仅在调试环节打印这些信息,那么可以将DEBUG设置为1个环节变量或配置变量,通过os包Getenv函数用于获取环境变量的值,可据此判断是否打印调试信息。

  • debug := os.Getenv("DEBUG")
// 调试HTTP请求
// net/http/httputil包的DumpRequestOut和DumpResponse函
// 数,可用于在调试过程中查看HTTP请求和响应,帮助查找BUG
package mainimport ("fmt""io/ioutil""log""net/http""net/http/httputil""os"
)func main() {debug := os.Getenv("DEBUG")client := http.Client{}request, err := http.NewRequest("GET", "https://ifconfig.io/ip", nil)if err != nil {log.Fatal(err)}request.Header.Add( // 通过设置请求头,设置了可接受的响应内容类型为json"Accept", "application/json")if debug == "1" {	// 打印请求debugRequest, err :=httputil.DumpRequestOut(request, true)if err != nil {log.Fatal(err)}fmt.Printf("%s", debugRequest)}response, err := client.Do(request)if err != nil {log.Fatal(err)}defer response.Body.Close()if debug == "1" {	// 打印响应debugResponse, err :=httputil.DumpResponse(response, true)if err != nil {log.Fatal(err)}fmt.Printf("%s", debugResponse)}resBody, err := ioutil.ReadAll(response.Body)if err != nil {log.Fatal(err)}fmt.Printf("%s", resBody)
}
// 打印输出:
GET /ip HTTP/1.1
Host: ifconfig.io
User-Agent: Go-http-client/1.1
Accept: application/json
Accept-Encoding: gzipHTTP/2.0 200 OK
Content-Length: 13
Alt-Svc: h3-24=":443"; ma=86400, h3-23=":443"; ma=86400
Cf-Cache-Status: DYNAMIC
Cf-Ray: 562461317d37d342-LAX
Content-Type: text/plain; charset=utf-8
Date: Sun, 09 Feb 2020 08:12:40 GMT
Expect-Ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
Set-Cookie: __cfduid=d9ad8e7d64d78d19d075953bebfb13afa1581235960; expires=Tue, 10-Mar-20 08:12:40 GMT; path=/; domain=.ifconfig.io; HttpOnly; SameSite=Laxxxx.xxx.xxx.xxx	客户端ip,已隐去
xxx.xxx.xxx.xxx

 3. 响应超时

客户端向服务器发送请求后,完全无法知道服务器会在多长时间内返回响应。

在系统的底层,有太多因素会对响应时间构成影响。

  • 在客户端一侧:
    • DNS查找速度
    • 创建TCP套接字的速度
    • 与服务器建立TCP连接的速度
    • TLS握手的速度(如果使用HTTPS)
    • 向服务器发送数据的速度
  • 在服务器一侧:
    • 重定向的速度
    • 业务处理的速度
    • 向客户端发送数据的速度

默认方式创建的客户端没有对响应设置超时,这意味着:

  • 如果服务器很久甚至永远没有向客户端返回响应,客户端将一直等待
  • 维持这条连接的内存和表示这个套接字的文件描述符,也将一直存在
  • 如果发出的多个请求都是这种情况,那么客户端的资源将会很快耗尽

建议为客户端设置响应超时,一旦超过时间还没有收到响应,即宣告错误

  • client := http.Client{Timeout: 1 * time.Second}

        在声明http.Client变量对象时,设置其Timeout字段值,例如设置响应超 时1秒钟 。

  • response, err := client.Do(request)

        继续使用client.Do发送请求,如果超过一秒钟还没收到来自服务器的响应,则返回错误。

// 处理响应超时
// HTTP客户端在向服务器发送请求后,完全无法知道何时能收到对方的响应。
// 建议设置一个超时时间,如果在指定的时间内没有收到响应,则返回错误
package main
import ("fmt" "io/ioutil" "log" "net/http" "net/http/httputil" "os" "time" 
)
func main() {debug := os.Getenv("DEBUG")client := http.Client{Timeout: 1 * time.Second}request, err := http.NewRequest("GET", "https://ifconfig.io/ip", nil)if err != nil {log.Fatal(err)}if debug == "1" {debugRequest, err :=httputil.DumpRequestOut(request, true)if err != nil {log.Fatal(err)}fmt.Printf("%s", debugRequest)}response, err := client.Do(request)if err != nil {log.Fatal(err)}defer response.Body.Close()if debug == "1" {debugResponse, err :=httputil.DumpResponse(response, true)if err != nil {log.Fatal(err)}fmt.Printf("%s", debugResponse)}resBody, err := ioutil.ReadAll(response.Body)if err != nil {log.Fatal(err)}fmt.Printf("%s", resBody)
}
// 打印输出:
2020/02/09 14:21:08 Get https://ifconfig.io/ip: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

使用Transport可以更精细化地控制超时,甚至为传输的每个阶段设置超时。此时,我们需要填充client结构体中的Transport字段,该字段值是由一个http导出类型(公有类型)Transport所创建的结构体变量,该结构体可包含多个字段,通过每个字段为传输的每个阶段设置单独的超时时间。

  • dl := net.Dialer{
  •     Timeout:   30 * time.Second,
  •     KeepAlive: 30 * time.Second,
  • }
  • tr := http.Transport{
  •     DialContext:           dl.DialContext,
  •     TLSHandshakeTimeout:   10 * time.Second,
  •     IdleConnTimeout:       90 * time.Second,
  •     ResponseHeaderTimeout: 10 * time.Second,
  •     ExpectContinueTimeout:  1 * time.Second,
  • }
  • client := http.Client{Transport: &tr}
// 精细化控制超时
// 使用Transport可以更精细化地控制超时,甚 
// 至为HTTP传输的每个阶段设置独立的超时
package mainimport ("fmt""io/ioutil""log""net""net/http""net/http/httputil""os""testing""time"
)func TestFineResponseTimeout(t *testing.T) {debug := os.Getenv("DEBUG")dl := net.Dialer{Timeout:   30 * time.Second,KeepAlive: 30 * time.Second,}tr := http.Transport{DialContext:           dl.DialContext,TLSHandshakeTimeout:   10 * time.Second,IdleConnTimeout:       90 * time.Second,ResponseHeaderTimeout: 10 * time.Second,ExpectContinueTimeout: 1 * time.Second,}client := http.Client{Transport: &tr}request, err := http.NewRequest("GET", "https://ifconfig.io/ip",nil)if err != nil {log.Fatal(err)}if debug == "1" {debugRequest, err :=httputil.DumpRequestOut(request, true)if err != nil {log.Fatal(err)}fmt.Printf("%s", debugRequest)}response, err := client.Do(request)if err != nil {log.Fatal(err)}defer response.Body.Close()if debug == "1" {debugResponse, err :=httputil.DumpResponse(response, true)if err != nil {log.Fatal(err)}fmt.Printf("%s", debugResponse)}resBody, err := ioutil.ReadAll(response.Body)if err != nil{log.Fatal(err)}fmt.Printf("%s",resBody)
}
// 打印输出:
2020/02/09 16:39:39 Get https://ifconfig.io/ip: dial tcp: lookup ifconfig.io: no such host //手动断网导致访问失败

http://www.yayakq.cn/news/202659/

相关文章:

  • 手机英文网站大全网站开发知识产权
  • 招投标网站网站建设办公软件销售技巧
  • 山东住房建设部官方网站威海建设集团网站首页
  • 网站做一些流量互换怎么做企业网站推广需要多少钱
  • 网络创作网站如何做直接打开网站的二维码
  • 网站安装源码霸气的网络公司名字
  • app展示网站模板html5凡科轻站小程序模板
  • 机械设备东莞网站建设建筑人才市场职称评审
  • 济南大型网站建设单位网站建设程序
  • 网站设计费网站icp备案信息是什么意思
  • 制造业网站建设室内设计师培训班多少钱
  • 青海省公路建设管理局网站创建wordpress主题
  • 网站开发解决方案云服务器哪家好
  • 建站之星多少钱wordpress如何cdn优化
  • 安徽专业网站建设设计电商网站设计公司有哪些
  • 公司网站推广方法深圳音乐制作公司
  • 手机网站开发公司边坝网站制作
  • 昔阳网站建设成都房产信息网 官网
  • 旅行网站开发背景帝国cms怎么做网站声明
  • 网站用什么软件seo排名优化潍坊网络营销招聘
  • 选择网站建设公司好ssh鲜花礼品网站建设
  • 网站网页跳转河南网站设计
  • 如何做内网网站WordPress主题改为html模板
  • ftp网站建立深圳设计网站招聘
  • 供求网站建设wordpress和站点
  • 建设高校网站的现实意义怎么设置wordpress头像
  • 山阴县2017建设局网站wordpress frp穿透
  • 做非洲出口的网站数字尾巴+wordpress
  • 网站建设多少钱专业小宽带怎样做视频网站
  • 怎么建设自己的网站电子商务网站建设与管理笔试