golang 设置web请求状态码操作
我就废话不多说了,大家还是直接看代码吧~
packagemain import( "net/http" ) funcmain(){ //路由处理绑定 http.HandleFunc("/",Hander) //监听8080端口 http.ListenAndServe(":8080",nil) } funcHander(whttp.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 //NewHTTPstatuscodesfromRFC6585.NotexportedyetinGo1.1. //Seediscussionathttps://codereview.appspot.com/7678043/ statusPreconditionRequired=428 statusTooManyRequests=429 statusRequestHeaderFieldsTooLarge=431 statusNetworkAuthenticationRequired=511 )
下面修改一下就是这个样子
packagemain import( "net/http" ) funcmain(){ //路由处理绑定 http.HandleFunc("/",Hander) //监听8080端口 http.ListenAndServe(":8080",nil) } funcHander(whttp.ResponseWriter,req*http.Request){ //设置http请求状态为500 w.WriteHeader(http.StatusInternalServerError) //写入页面数据 w.Write([]byte("xiaochuan")) }
补充:gostatus.go状态码定义
status.go使用了一个map集合定义了http的响应状态码
具体的参考如下
//Copyright2009TheGoAuthors.Allrightsreserved. //UseofthissourcecodeisgovernedbyaBSD-style //licensethatcanbefoundintheLICENSEfile. packagehttp //HTTPstatuscodes,definedinRFC2616. 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 StatusPreconditionRequired=428 StatusTooManyRequests=429 StatusRequestHeaderFieldsTooLarge=431 StatusUnavailableForLegalReasons=451 StatusInternalServerError=500 StatusNotImplemented=501 StatusBadGateway=502 StatusServiceUnavailable=503 StatusGatewayTimeout=504 StatusHTTPVersionNotSupported=505 StatusNetworkAuthenticationRequired=511 ) varstatusText=map[int]string{ StatusContinue:"Continue", StatusSwitchingProtocols:"SwitchingProtocols", StatusOK:"OK", StatusCreated:"Created", StatusAccepted:"Accepted", StatusNonAuthoritativeInfo:"Non-AuthoritativeInformation", StatusNoContent:"NoContent", StatusResetContent:"ResetContent", StatusPartialContent:"PartialContent", StatusMultipleChoices:"MultipleChoices", StatusMovedPermanently:"MovedPermanently", StatusFound:"Found", StatusSeeOther:"SeeOther", StatusNotModified:"NotModified", StatusUseProxy:"UseProxy", StatusTemporaryRedirect:"TemporaryRedirect", StatusBadRequest:"BadRequest", StatusUnauthorized:"Unauthorized", StatusPaymentRequired:"PaymentRequired", StatusForbidden:"Forbidden", StatusNotFound:"NotFound", StatusMethodNotAllowed:"MethodNotAllowed", StatusNotAcceptable:"NotAcceptable", StatusProxyAuthRequired:"ProxyAuthenticationRequired", StatusRequestTimeout:"RequestTimeout", StatusConflict:"Conflict", StatusGone:"Gone", StatusLengthRequired:"LengthRequired", StatusPreconditionFailed:"PreconditionFailed", StatusRequestEntityTooLarge:"RequestEntityTooLarge", StatusRequestURITooLong:"RequestURITooLong", StatusUnsupportedMediaType:"UnsupportedMediaType", StatusRequestedRangeNotSatisfiable:"RequestedRangeNotSatisfiable", StatusExpectationFailed:"ExpectationFailed", StatusTeapot:"I'mateapot", StatusPreconditionRequired:"PreconditionRequired", StatusTooManyRequests:"TooManyRequests", StatusRequestHeaderFieldsTooLarge:"RequestHeaderFieldsTooLarge", StatusUnavailableForLegalReasons:"UnavailableForLegalReasons", StatusInternalServerError:"InternalServerError", StatusNotImplemented:"NotImplemented", StatusBadGateway:"BadGateway", StatusServiceUnavailable:"ServiceUnavailable", StatusGatewayTimeout:"GatewayTimeout", StatusHTTPVersionNotSupported:"HTTPVersionNotSupported", StatusNetworkAuthenticationRequired:"NetworkAuthenticationRequired", } //返回httpcode对应的状态码描述信息 //返回空字符串表示状态码unknown funcStatusText(codeint)string{ returnstatusText[code] }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。