本文的目的是介绍在http.HandleFunc内记录对传入HTTP请求的响应的详细情况,特别关注在http协议的“请求/响应”交互模型中的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为
本文的目的是介绍在http.HandleFunc内记录对传入HTTP请求的响应的详细情况,特别关注在http协议的“请求/响应”交互模型中的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解在http.HandleFunc内记录对传入HTTP请求的响应的机会,同时也不会遗漏关于Android 使用 httpClient 取消http请求的方法、ASP.NET处理HTTP请求的流程:IHttpModule、IHttpHandler与管道事件、go http http.Handle 和 http.HandleFunc 区别、golang http HandleFunc的知识。
本文目录一览:- 在http.HandleFunc内记录对传入HTTP请求的响应(在http协议的“请求/响应”交互模型中)
- Android 使用 httpClient 取消http请求的方法
- ASP.NET处理HTTP请求的流程:IHttpModule、IHttpHandler与管道事件
- go http http.Handle 和 http.HandleFunc 区别
- golang http HandleFunc
在http.HandleFunc内记录对传入HTTP请求的响应(在http协议的“请求/响应”交互模型中)
这是In go中的后续问题,如何检查写入http.ResponseWriter的http响应因为那里的解决方案需要伪造一个请求,所以对于单元测试非常有效,但不适用于实时服务器。
我想将我的Web服务响应于从用户收到的请求而返回的HTTP响应转储到日志文件(或控制台)中。输出应该告诉我标题是什么和JSON有效负载。
如何做到这一点?
如果有一个使用http.ResponseWriter作为参数而不是http.Response的httputil.DumpResponse等效项,那将是完美的,但是目前我只能从http.ResponseWriter访问标头
r = mux.NewRouter()
r.HandleFunc("/path",func (w http.ResponseWriter,r *http.Request) {
fmt.Printf("r.HandleFunc /path\n")
resp := server.NewResponse()
defer resp.Close()
r.ParseForm()
// Server does some work here
// ...
// Insert debug code here,something like
//
// dump = http.DumpResponseFromWriter(w)
// fmt.Printf("%s\n",dump)
});
http.Handle("/path",r)
Android 使用 httpClient 取消http请求的方法
其实apache还是提供了释放 连接资源的方法的,不过是埋得深了点。
httpClient.getConnectionManager().shutdown();
这个shutdown并不是将手机网络断掉,而是将建立Http连接请求时所分配的资源释放掉。
ASP.NET处理HTTP请求的流程:IHttpModule、IHttpHandler与管道事件
一、ASP.NET处理管道
- Asp.net处理管道的第一步是创建HttpWorkerRequest对象,它包含于当前请求有关的所有信息。
- HttpWorkerRequest把请求传递给HttpRuntime类的静态ProcessRequest方法。HttpRuntime首先要做的事是创建HttpContext对象,并用HttpWorkerRequest进行初始化。
- 创建了HttpContext实例之后,HttpRuntime类就通过调用HttpApplicationFactory的静态GetApplicationInstance()方法,为该应用程序请求HttpApplication派生类的一个示例。GetApplicationInstance()方法要么创建一个HttpApplication类的一个新实例,要么从应用程序对象池中取出一个实例。
- 在创建完成HttpApplication实例之后,就对它进行初始化,并在初始化期间分配应用程序定义的所
- 有模块。模块式实现IHttpModule接口的类,作用就是为了实现那经典的19个标准处理事件。
- 在创建了模块之后,HttpRuntime类通过调用它的BeginProcessRequest方法,要求最新检索到的HttpApplication类对当前请求提供服务。然后,为当前请求找到合适的处理程序工厂。
- 创建处理程序,传递当前HttpContext,一旦ProcessRequest方法返回,请求完成。
二、IHttpHandler
HttpHandler是asp.net真正处理Http请求的地方。在这个HttpHandler容器中,ASP.NET Framework才真正地对客户端请求的服务器页面做出编译和执行,并将处理过后的信息附加在HTTP请求信息流中再次返回到HttpModule中。
当一个HTTP请求经过HttpModule容器传递到HttpHandler容器中时,ASP.NET Framework会调用HttpHandler的ProcessRequest成员方法来对这个HTTP请求进行真正的处理。并将处理完成的结果继续经由HttpModule传递下去,直至到达客户端。
HttpHandler与HttpModule不同,一旦定义了自己的HttpHandler类,那么它对系统的HttpHandler的关系将是“覆盖”关系。
应用1:图片防盗链(实现一个自定义的IHttpHandler)
第一:定义一个实现了IHttpHandler的类,并且实现其ProcessRequest方法。在一个HttpHandler容器中如果需要访问Session,必须实现IRequiresSessionState接口,这只是一个标记接口,没有任何方法。
public class PictureHttpHandler : IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { //站点的域名 string myDomain = "localhost"; if (context.Request.UrlReferrer == null || context.Request.UrlReferrer.Host.ToLower().IndexOf(myDomain) < 0) { //如果是通过浏览器直接访问或者是通过其他站点访问过来的,则显示“资源不存在”图片 context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(context.Request.PhysicalApplicationPath + "/images/noimg.jpg"); } else { //如果是通过站内访问的,这正常显示图片 context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(context.Request.PhysicalPath); } } }
第二:在web.config中注册这个类,并且指定Handler处理的请求类型,把此节点插入system.web节点中
<httpHandlers> <!--path中指定的是执行type中HttpHandler的访问路径。此路径可以带后缀也可以不带后缀。如果path配置为*,则会对所有的请求执行此HttpHandler--> <add verb="*" path="*.jpg" type="MyHttpHandler.PictureHttpHandler,MyHttpHandler"/> </httpHandlers>
正常访问default页面时:
通过图片地址直接访问时:
应用2、生成验证码
public class ValidateCodeHttpHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/gif"; //建立Bitmap对象,绘图 Bitmap basemap = new Bitmap(200, 60); Graphics graph = Graphics.FromImage(basemap); graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60); Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel); Random r = new Random(); string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ"; string letter; StringBuilder s = new StringBuilder(); //添加随机的五个字母 for (int x = 0; x < 5; x++) { letter = letters.Substring(r.Next(0, letters.Length - 1), 1); s.Append(letter); graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15)); } //混淆背景 Pen linePen = new Pen(new SolidBrush(Color.Black), 2); for (int x = 0; x < 6; x++) graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59))); //将图片保存到输出流中 basemap.Save(context.Response.OutputStream, ImageFormat.Gif); //context.Session["CheckCode"] = s.ToString(); //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片 context.Response.End(); } public bool IsReusable { get { return true; } } }
把下面的项加到web.config中的httphandler节点中:
<add verb="*" path="validatevode" type="MyHttpHandler.ValidateCodeHttpHandler,MyHttpHandler"/>
访问validatevode时:
三、自定义HttpModule:
每次请求的开始和结束定义的HttpModule。
在Asp.net中,创建在System.Web命名空间下的IHttpModule接口专门用来定义HttpApplication对象的事件处理。实现IHttpModule接口的类称为HttpModule(Http模块)。
1、通过IHttpModule创建HttpApplication的事件处理程序
public class ModuleExample : IHttpModule { public void Init(System.Web.HttpApplication application) { application.PostAuthenticateRequest += (sender, args) => { HttpContext context = ((HttpApplication)sender).Context; context.Response.Write("请求PostAuthenticate"); }; application.BeginRequest += (sender, args) => { HttpContext context = ((HttpApplication)sender).Context; context.Response.Write("请求到达"); }; application.EndRequest += (sender, args) => { HttpContext context = ((HttpApplication)sender).Context; context.Response.Write("请求结束"); }; } public void Dispose() { throw new NotImplementedException(); } }
2、注册HttpModule
在Asp.net中,实现IHttpModule接口只是实现HttpModule的第一步,在Asp.net中所使用的HttpModule还必须在网站配置文件中进行注册才能真正生效,并在Asp.net中使用。
<system.webServer> <modules> <add name="ModuleExample" type="Samples.ModeleExample"> </modules> </system.webServer>
3、常见的HttpModule
在Asp.net中,已经预定义了许多HttpModule,甚至已经在服务器的网站配置文件中进行了注册,在系统文件夹C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config中看到已经注册的HttpModule如下:
<httpModules> <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" /> <add name="Session" type="System.Web.SessionState.SessionStateModule" /> <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" /> <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" /> <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" /> <add name="RoleManager" type="System.Web.Security.RoleManagerModule" /> <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" /> <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" /> <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" /> <add name="Profile" type="System.Web.Profile.ProfileModule" /> <add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" /> <add name="ScriptModule-4.0" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </httpModules>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
- ASP.NET Core使用IHttpClientFactory发出HTTP请求
- ASP.NET Core扩展库之Http请求模拟功能的使用
- 在ASP.NET Core中用HttpClient发送POST, PUT和DELETE请求
- ASP.NET Core中间件计算Http请求时间示例详解
- ASP.NET Core中调整HTTP请求大小的几种方法详解
- asp.net 请求输入到输出的全过程及httpHandler和httpModuler详细介绍
- Asp.net使用HttpModule压缩并删除空白Html请求的实现代码
- asp.net 截取Http请求的实现代码
go http http.Handle 和 http.HandleFunc 区别
使用net/http包的时候这个区别有点糊涂,所以查了一下 做一下总结
区别
http.Handle
func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }
第二个参数是Handler这个接口, 这个接口有一个ServeHTTP()的方法
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
所以这个方法使用的时候需要自己去定义struct实现这个Handler接口。
package main
import (
"net/http"
"log"
)
type httpServer struct {
}
func (server httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.URL.Path))
}
func main() {
var server httpServer
http.Handle("/", server)
log.Fatal(http.ListenAndServe("localhost:9000", nil))
}
http.HandleFunc
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, handler)
}
这个第二个参数是一个方法,参数是ResponseWriter, 和 *Request 所以使用的时候需要传方法。
package main
import (
"net/http"
"log"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.URL.Path))
})
log.Fatal(http.ListenAndServe("localhost:9000", nil))
}
所以一般使用HandleFunc就可以了。
参考
Go语言的“http.Handle”和“http.HandleFunc”
golang http HandleFunc
golang http的handle模块(一般也称为钩子模块),通过高级语言的匿名函数很容易实现这种内嵌功能的handle
我们一般这样使用golang的http HandleFunc来为http的server端做相应的处理
/*********************************************/ http.HandleFunc("/",xxx_FUN) err:=http.ListenAndServe(":8080",nil) iferr!=nil{ log.Fatal("ListenAndServe:",err) } /*********************************************/
我们再深入源码仔细看看http.HandleFunc的实现
funcHandleFunc(patternstring,handlerfunc(ResponseWriter,*Request)){ DefaultServeMux.HandleFunc(pattern,handler) } //NewServeMuxallocatesandreturnsanewServeMux. varDefaultServeMux=NewServeMux() funcNewServeMux()*ServeMux{return&ServeMux{m:make(map[string]muxEntry)}} typeServeMuxstruct{ musync.RWMutex//一个读写锁 mmap[string]muxEntry//一个path(patterns)的映射map hostsbool//whetheranypatternscontainhostnames }
再来看看ListenAndServe的具体实现
funcListenAndServe(addrstring,handlerHandler)error{ server:=&Server{Addr:addr,Handler:handler} returnserver.ListenAndServe() } func(srv*Server)ListenAndServe()error{ addr:=srv.Addr ifaddr==""{ addr=":http" } ln,err:=net.Listen("tcp",addr) iferr!=nil{ returnerr } returnsrv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)}) } //ServeacceptsincomingconnectionsontheListenerl,creatinga //newservicegoroutineforeach.Theservicegoroutinesreadrequestsand //thencallsrv.Handlertoreplytothem. func(srv*Server)Serve(lnet.Listener)error{ deferl.Close() vartempDelaytime.Duration//howlongtosleeponacceptfailure for{ rw,e:=l.Accept() ife!=nil{ ifne,ok:=e.(net.Error);ok&&ne.Temporary(){ iftempDelay==0{ tempDelay=5*time.Millisecond }else{ tempDelay*=2 } ifmax:=1*time.Second;tempDelay>max{ tempDelay=max } srv.logf("http:Accepterror:%v;retryingin%v",e,tempDelay) time.Sleep(tempDelay) continue } returne } tempDelay=0 c,err:=srv.newConn(rw) iferr!=nil{ continue } c.setState(c.rwc,StateNew)//beforeServecanreturn goc.serve()//看来这个c.serve是处理的入口 } }
看来这个c.serve是处理的入口
//Serveanewconnection. func(c*conn)serve(){ origConn:=c.rwc//copyitbeforeit'ssetnilonCloSEOrHijack deferfunc(){ iferr:=recover();err!=nil{ constsize=64<<10 buf:=make([]byte,size) buf=buf[:runtime.Stack(buf,false)] c.server.logf("http:panicserving%v:%v\n%s",c.remoteAddr,err,buf) } if!c.hijacked(){ c.close() c.setState(origConn,StateClosed) } }() iftlsConn,ok:=c.rwc.(*tls.Conn);ok{ ifd:=c.server.ReadTimeout;d!=0{ c.rwc.SetReadDeadline(time.Now().Add(d)) } ifd:=c.server.WriteTimeout;d!=0{ c.rwc.SetWriteDeadline(time.Now().Add(d)) } iferr:=tlsConn.Handshake();err!=nil{ c.server.logf("http:TLShandshakeerrorfrom%s:%v",c.rwc.RemoteAddr(),err) return } c.tlsstate=new(tls.ConnectionState) *c.tlsstate=tlsConn.ConnectionState() ifproto:=c.tlsstate.NegotiatedProtocol;validNPN(proto){ iffn:=c.server.TLSNextProto[proto];fn!=nil{ h:=initNPNRequest{tlsConn,serverHandler{c.server}} fn(c.server,tlsConn,h) } return } } for{ w,err:=c.readRequest() ifc.lr.N!=c.server.initialLimitedReaderSize(){ //Ifwereadanybytesoffthewire,we'reactive. c.setState(c.rwc,StateActive) } iferr!=nil{ iferr==errTooLarge{ //TheirHTTPclientmayormaynotbe //abletoreadthisifwe're //respondingtothemandhangingup //whilethey'restillwritingtheir //request.Undefinedbehavior. io.WriteString(c.rwc,"HTTP/1.1413RequestEntityTooLarge\r\n\r\n") c.closeWriteAndWait() break }elseiferr==io.EOF{ break//Don'treply }elseifneterr,ok:=err.(net.Error);ok&&neterr.Timeout(){ break//Don'treply } io.WriteString(c.rwc,"HTTP/1.1400BadRequest\r\n\r\n") break } //Expect100Continuesupport req:=w.req ifreq.expectsContinue(){ ifreq.ProtoAtLeast(1,1)&&req.ContentLength!=0{ //WraptheBodyreaderwithonethatrepliesontheconnection req.Body=&expectContinueReader{readCloser:req.Body,resp:w} } req.Header.Del("Expect") }elseifreq.Header.get("Expect")!=""{ w.sendExpectationFailed() break } //HTTPcannothavemultiplesimultaneousactiverequests.[*] //Untiltheserverrepliestothisrequest,itcan'treadanother,//sowemightaswellrunthehandlerinthisgoroutine. //[*]Notstrictlytrue:HTTPpipelining.WeCouldletthemallprocess //inparalleleveniftheirresponsesneedtobeserialized. serverHandler{c.server}.ServeHTTP(w,w.req)//这个是入口 ifc.hijacked(){ return } w.finishRequest() ifw.closeAfterReply{ ifw.requestBodyLimitHit{ c.closeWriteAndWait() } break } c.setState(c.rwc,StateIdle) } }
Handler处理的入口就是serverHandler{c.server}.ServerHTTP(w,w.req),最终到HandleFunc的执行
func(mux*ServeMux)Handler(r*Request)(hHandler,patternstring){ ifr.Method!="CONNECT"{ ifp:=cleanPath(r.URL.Path);p!=r.URL.Path{ _,pattern=mux.handler(r.Host,p)//接下来处理 url:=*r.URL url.Path=p returnRedirectHandler(url.String(),StatusMovedPermanently),pattern } } returnmux.handler(r.Host,r.URL.Path)//接下来处理 } func(mux*ServeMux)handler(host,pathstring)(hHandler,patternstring){ mux.mu.RLock() defermux.mu.RUnlock() //Host-specificpatterntakesprecedenceovergenericones ifmux.hosts{ h,pattern=mux.match(host+path) } ifh==nil{ h,pattern=mux.match(path) } ifh==nil{ h,pattern=NotFoundHandler(),""//如果handler对应的匿名函数为空,则返回默认的匿名函数 } return } //ServeHTTPdispatchestherequesttothehandlerwhose //patternmostcloselymatchestherequestURL. func(mux*ServeMux)ServeHTTP(wResponseWriter,r*Request){ ifr.RequestURI=="*"{ ifr.ProtoAtLeast(1,1){ w.Header().Set("Connection","close") } w.WriteHeader(StatusBadRequest) return } h,_:=mux.Handler(r)//接下来处理 h.ServeHTTP(w,r)//接下来处理 } //接下来就初始时候执行的操作 func(mux*ServeMux)HandleFunc(patternstring,*Request)){ mux.Handle(pattern,HandlerFunc(handler)) } func(mux*ServeMux)Handle(patternstring,handlerHandler){//处理pattern mux.mu.Lock() defermux.mu.Unlock() ifpattern==""{ panic("http:invalidpattern"+pattern) } ifhandler==nil{ panic("http:nilhandler") } ifmux.m[pattern].explicit{ panic("http:multipleregistrationsfor"+pattern) } mux.m[pattern]=muxEntry{explicit:true,h:handler,pattern:pattern}//设置ServeMux的map ifpattern[0]!='/'{ mux.hosts=true } //Helpfulbehavior: //Ifpatternis/tree/,insertanimplicitpermanentredirectfor/tree. //Itcanbeoverriddenbyanexplicitregistration. n:=len(pattern) ifn>0&&pattern[n-1]=='/'&&!mux.m[pattern[0:n-1]].explicit{ //Ifpatterncontainsahostname,stripitanduseremaining //pathforredirect. path:=pattern ifpattern[0]!='/'{ //Inpattern,atleastthelastcharacterisa'/',so //strings.Indexcan'tbe-1. path=pattern[strings.Index(pattern,"/"):] } mux.m[pattern[0:n-1]]=muxEntry{h:RedirectHandler(path,pattern:pattern} } }
最后再看看通过mux匹配获取对应的map的操作:
func(mux*ServeMux)match(pathstring)(hHandler,patternstring){ varn=0 fork,v:=rangemux.m{ if!pathMatch(k,path){//匹配 continue } ifh==nil||len(k)>n{ n=len(k) h=v.h pattern=v.pattern } } return }
关于在http.HandleFunc内记录对传入HTTP请求的响应和在http协议的“请求/响应”交互模型中的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Android 使用 httpClient 取消http请求的方法、ASP.NET处理HTTP请求的流程:IHttpModule、IHttpHandler与管道事件、go http http.Handle 和 http.HandleFunc 区别、golang http HandleFunc等相关知识的信息别忘了在本站进行查找喔。
本文标签: