zoukankan      html  css  js  c++  java
  • golang 设置web请求状态码

    package main
    
    import (
    	"net/http"
    )
    
    func main() {
    	//路由处理绑定
    	http.HandleFunc("/", Hander)
    	//监听8080端口
    	http.ListenAndServe(":8080", nil)
    }
    func Hander(w http.ResponseWriter, req *http.Request) {
    	//设置 http请求状态
    	w.WriteHeader(500)
    	//写入页面数据
    	w.Write([]byte("xiaochuan"))
    }
    

    你也可以用http 包里面的常量 我这边直接写数字方便理解而已

    const (
    	StatusContinue           = 100
    	StatusSwitchingProtocols = 101
    
    	StatusOK                   = 200
    	StatusCreated              = 201
    	StatusAccepted             = 202
    	StatusNonAuthoritativeInfo = 203
    	StatusNoContent            = 204
    	StatusResetContent         = 205
    	StatusPartialContent       = 206
    
    	StatusMultipleChoices   = 300
    	StatusMovedPermanently  = 301
    	StatusFound             = 302
    	StatusSeeOther          = 303
    	StatusNotModified       = 304
    	StatusUseProxy          = 305
    	StatusTemporaryRedirect = 307
    
    	StatusBadRequest                   = 400
    	StatusUnauthorized                 = 401
    	StatusPaymentRequired              = 402
    	StatusForbidden                    = 403
    	StatusNotFound                     = 404
    	StatusMethodNotAllowed             = 405
    	StatusNotAcceptable                = 406
    	StatusProxyAuthRequired            = 407
    	StatusRequestTimeout               = 408
    	StatusConflict                     = 409
    	StatusGone                         = 410
    	StatusLengthRequired               = 411
    	StatusPreconditionFailed           = 412
    	StatusRequestEntityTooLarge        = 413
    	StatusRequestURITooLong            = 414
    	StatusUnsupportedMediaType         = 415
    	StatusRequestedRangeNotSatisfiable = 416
    	StatusExpectationFailed            = 417
    	StatusTeapot                       = 418
    
    	StatusInternalServerError     = 500
    	StatusNotImplemented          = 501
    	StatusBadGateway              = 502
    	StatusServiceUnavailable      = 503
    	StatusGatewayTimeout          = 504
    	StatusHTTPVersionNotSupported = 505
    
    	// New HTTP status codes from RFC 6585. Not exported yet in Go 1.1.
    	// See discussion at https://codereview.appspot.com/7678043/
    	statusPreconditionRequired          = 428
    	statusTooManyRequests               = 429
    	statusRequestHeaderFieldsTooLarge   = 431
    	statusNetworkAuthenticationRequired = 511
    )
    

    下面修改一下就是这个样子

    package main
    
    import (
    	"net/http"
    )
    
    func main() {
    	//路由处理绑定
    	http.HandleFunc("/", Hander)
    	//监听8080端口
    	http.ListenAndServe(":8080", nil)
    }
    func Hander(w http.ResponseWriter, req *http.Request) {
    	//设置 http请求状态 为500
    	w.WriteHeader(http.StatusInternalServerError)
    
    	//写入页面数据
    	w.Write([]byte("xiaochuan"))
    }
    
  • 相关阅读:
    apache和tomcat有什么不同,为什么要整合apache 和tomcat?
    servlet
    关于Spring配置文件xml文档的schema约束
    request对象和response对象
    多线程
    数据结构得到连续数据的手段java Enumeration
    程序员八荣八耻
    windows更改MySQL存储路径
    Tomcat源码学习(1)
    Tomcat源码学习(2)——启动过程分析
  • 原文地址:https://www.cnblogs.com/enumx/p/12326618.html
Copyright © 2011-2022 走看看