博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# HttpDirect
阅读量:7112 次
发布时间:2019-06-28

本文共 4684 字,大约阅读时间需要 15 分钟。

private HttpListener _httpListener;private HttpTunnelClient(ushort listenPort, Uri serverUri, NetworkCredential credential, IPEndPoint remoteEndPoint = null, SocksProxyType? runType = null)        {_httpListener = new HttpListener();                _httpListener.Prefixes.Add(string.Format("http://*:{0}/", listenPort));                _httpListener.Start();                _httpListener.BeginGetContext(this.GetContextCallback, null);}private void GetContextCallback(IAsyncResult ar)        {            if (_httpListener == null)            {                return;            }            _httpListener.BeginGetContext(this.GetContextCallback, null);            var context = _httpListener.EndGetContext(ar);            this.DirectHttp(context);        }        private void DirectHttp(HttpListenerContext context)        {            var Request = context.Request;            var Response = context.Response;            string destUri = Request.RawUrl;            try            {                var tunnel = this.CreateTunnel(ClientNull, TunnelCommand.HttpDirect);                tunnel.Headers[xHttpHandler.AgentDirect] = destUri;                tunnel.Headers[xHttpHandler.AgentCommand] = ((int)TunnelCommand.HttpDirect).ToString();                tunnel.Form.Remove(xHttpHandler.AgentCommand);                //tunnel.SetProxy(xHttpHandler.GoAgent);                using (var tResponse = tunnel.GetResponseWith(new HttpRequestWrapper(Request)))                {                    Response.StatusCode = (int)tResponse.StatusCode;                    Response.StatusDescription = tResponse.StatusDescription;                    Response.KeepAlive = tResponse.Headers[HttpResponseHeader.Connection] != "close";                    if (tResponse.ContentLength >= 0)                    {                        Response.ContentLength64 = tResponse.ContentLength;                    }                    Response.ContentType = tResponse.ContentType;                    foreach (string header in HttpRequestWrapper.SystemHeaders)                    {                        tResponse.Headers.Remove(header);                    }                    Response.Headers.Add(tResponse.Headers);                    var tStream = tResponse.GetResponseStream();                    tStream.FixedCopyTo(Response.OutputStream);                }                this.OutWrite("{0} \"{1} {2}\" {3}\t{4}bytes.", Request.LocalEndPoint, Request.HttpMethod, destUri,                    Response.StatusCode, Response.ContentLength64);                //如果写入未完成(异常等情况)就调用Close()则会抛“在写入所有字节之前不能关闭流”异常。                Response.Close();            }            catch (WebException ex)            {                TunnelExceptionHandler.Handle(ex, string.Format("DirectHttp{0}={1}", Request.HttpMethod, destUri));            }            catch (Exception ex)            {                TunnelExceptionHandler.Handle(ex, string.Format("DirectHttp{0}={1}", Request.HttpMethod, destUri));            }        }

 

private void Direct2GoAgent(HttpContext context, Uri destUri)        {            context.Server.ScriptTimeout = Timeout;            HttpRequest Request = context.Request;            HttpResponse Response = context.Response;            Request.Headers.Remove(HttpRequestHeader.Authorization.ToString());            Request.Headers.Remove(AgentSock);            Request.Headers.Remove(AgentDirect);            Request.Headers.Remove(AgentCommand);            var client = new HttpClient(destUri);            client.SetRequest(destUri);            client.SetProxy(GoAgent);            try            {                using (var cResponse = client.GetResponseWith(new System.Web.HttpRequestWrapper(Request)))                {                    Response.StatusCode = (int)cResponse.StatusCode;                    Response.StatusDescription = cResponse.StatusDescription;                    Response.ContentType = cResponse.ContentType;                    Response.Headers.Add(cResponse.Headers);                    var cStream = cResponse.GetResponseStream();                    cStream.FixedCopyTo(Response.OutputStream, (int)cResponse.ContentLength);                    if (Response.IsClientConnected)                    {                        Response.Flush();                    }                }            }            catch (WebException ex)            {                TunnelExceptionHandler.Handle(ex, string.Format("Direct2GoAgent={0}", destUri));            }            catch (Exception ex)            {                TunnelExceptionHandler.Handle(ex, string.Format("Direct2GoAgent={0}", destUri));            }        }

 

转载于:https://www.cnblogs.com/Googler/archive/2013/05/12/3073665.html

你可能感兴趣的文章
HTML 斜线 表头
查看>>
如何显示XShell的最大行数
查看>>
Android 签名出错
查看>>
Dao层系列-1-Hibernate上手
查看>>
spring cloud gateway 跨域设置
查看>>
javascript遍历json对象数据的方法
查看>>
Jaxb 完全手册
查看>>
Velocity Date format
查看>>
SVN修改用户名与密码
查看>>
Java集合-Collection详解
查看>>
moment.js 时间处理
查看>>
如何判断滚动条滚到页面底部并执行事件
查看>>
php数据库数据转换为js中的json对象
查看>>
Maven tomcat7-maven-plugin 部署Maven Web 项目
查看>>
javascript 学习笔记 【数组迭代方法】
查看>>
linux的shell命令检测某个java程序是否执行
查看>>
oracle sum()over函数的使用
查看>>
win7安装mongoose/socketio报错未能加载vcbuild.exe
查看>>
thinkphp模版调用函数方法
查看>>
Linux环境下将log4j的日志存储到mongoDB
查看>>