C#實現會員內容的轉發

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace Simulation
{
    class NewWebResponse
    {
        private HttpWebResponse _response;
        private string _content;

        public HttpWebResponse Response
        {
            get
            {
                return _response;
            }

            set
            {
                _response = value;
            }
        }

        public string Content
        {
            get
            {
                return _content;
            }

            set
            {
                _content = value;
            }
        }
    }
    class Program
    {
        static CookieContainer cookie;
        static void Main(string[] args)
        {
            cookie = new CookieContainer();
            Thread thread = new Thread(AcceptInfo1);
            thread.IsBackground = true;
            thread.Start();
            Console.ReadLine();
        }

        static void AcceptInfo1()
        {
            HttpListener listerner = new HttpListener();
            listerner.Prefixes.Add("http://127.0.0.1:9999/");
            listerner.Start();
            while (true)
            {
                HttpListenerContext ctx = listerner.GetContext();
                try
                {
                     if (ctx.Request.Url.AbsoluteUri == "http://127.0.0.1:9999/")
                    {
                        var nresponseg = resutlRequest("host/xxx/login", string.Format("name={0}&pwd={1}", "admin", "123"), ctx.Request);
                        ctx.Response.ContentType = nresponseg.Response.ContentType;
                        using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.GetEncoding("GB2312")))
                        {
                            writer.Write(nresponseg.Content);
                            writer.Close();
                            ctx.Response.Close();
                        }
                    }
                    else
                    {
                        var rstring = "";
                        using (StreamReader read = new StreamReader(ctx.Request.InputStream))
                        {
                            rstring = read.ReadToEnd();
                            read.Close();
                        }
                        //替換網頁中的連接地址指向自己的主機地址
                        var nresponseg = resutlRequest(ctx.Request.Url.AbsoluteUri.Replace("127.0.0.1:9999", "host"), rstring, ctx.Request);
                        ctx.Response.ContentType = nresponseg.Response.ContentType;
                        using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.GetEncoding("GB2312")))
                        {
                            writer.Write(nresponseg.Content);
                            writer.Close();
                            ctx.Response.Close();
                        }
                    }

                }
                catch (Exception ex)
                {

                    // throw ex;
                }
            }
        }
        private static NewWebResponse resutlRequest(string url, string postData, HttpListenerRequest request)
        {
            try
            {
                HttpWebRequest nrequest = (HttpWebRequest)WebRequest.Create(url);
                if (request.Url.AbsoluteUri == "http://127.0.0.1:9999/")
                {
                    nrequest.Method = "Post";
                    nrequest.ContentType = "application/x-www-form-urlencoded; charset=gbk";
                }

                else
                {
                    nrequest.Method = request.HttpMethod;
                    nrequest.ContentType = request.ContentType;
                }
                nrequest.Accept = request.Headers["Accept"];
                nrequest.Headers["Accept-Encoding"] = request.Headers["Accept-Encoding"];
                nrequest.Headers["Accept-Language"] = request.Headers["Accept-Language"];
                nrequest.UserAgent = request.UserAgent;
                nrequest.AllowAutoRedirect = true;//x運行重定向
                nrequest.CookieContainer = cookie;
                nrequest.KeepAlive = true;//建立持久性連接
                //表單提交
                if (nrequest.Method.ToUpper() != "GET")
                {
                    byte[] bytepostData = Encoding.GetEncoding("GB2312").GetBytes(postData);
                    nrequest.ContentLength = bytepostData.Length;
                    using (Stream requestStm = nrequest.GetRequestStream())
                    {
                        requestStm.Write(bytepostData, 0, bytepostData.Length);
                    }
                }
                HttpWebResponse response = (HttpWebResponse)nrequest.GetResponse();
                string retString = "";
                using (Stream myResponseStream = response.GetResponseStream())
                {
                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("GB2312"));
                    retString = myStreamReader.ReadToEnd();
                }
                NewWebResponse nresponse = new NewWebResponse();
                //替換回原理的地址
                retString = retString.Replace("host", "127.0.0.1:9999");
                nresponse.Response = response;
                nresponse.Content = retString;
                return nresponse;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw ex;
            }
        }
    }
}
由於編碼轉爲string會破壞圖片的格式,所以這裏只能正常顯示網頁的文字信息,如果想中轉圖片,可以嘗試用直接轉發字節流,但是這樣修改網頁中資源的地址可能就比較麻煩了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章