.Net網(wǎng)頁轉(zhuǎn)發(fā)工具

用法
http://localhost:端口號/轉(zhuǎn)發(fā)網(wǎng)址

        private static string port = "";
        private static HttpListener httpListener;
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                port = args[1];
            }
            else
            {
                Console.WriteLine("請輸入訪問端口:");
                port = Console.ReadLine();
            }
            Console.WriteLine($"返回端口:{port}");
            Start();
            Console.ReadLine();
        }


        private static void Start()
        {
            try
            {
                Program.httpListener = new HttpListener();
                Program.httpListener.Prefixes.Add("http://+:" + Program.port + "/");
                Program.httpListener.Start();
                Program.httpListener.BeginGetContext(new AsyncCallback(Program.Result), (object)null);
                Console.WriteLine("啟動成功");
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("啟動失敗:" + ex.ToString());
            }
        }

        private static void Result(IAsyncResult ar)
        {
            Program.httpListener.BeginGetContext(new AsyncCallback(Program.Result), (object)null);
            HttpListenerContext context = Program.httpListener.EndGetContext(ar);
            HttpListenerRequest request = context.Request;
            HttpListenerResponse response = context.Response;
            context.Response.ContentType = "text/plain;charset=UTF-8";
            context.Response.AddHeader("Content-type", "text/plain");
            context.Response.ContentEncoding = Encoding.UTF8;
            byte[] bytes = Encoding.UTF8.GetBytes(Program.HandleRequest(request, response));
            try
            {
                using (Stream outputStream = response.OutputStream)
                    outputStream.Write(bytes, 0, bytes.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine("網(wǎng)絡(luò)蹦了:" + ex.ToString());
            }
        }

        private static string HandleRequest(HttpListenerRequest request, HttpListenerResponse response)
        {
            try
            {
                string str = "http:/" + request.RawUrl;
                Console.WriteLine("用戶發(fā)起【" + request.HttpMethod + "】" + request.Url.ToString() + "代理地址至" + str);
                if (request.HttpMethod == "POST")
                {
                    List<byte> byteList = new List<byte>();
                    byte[] buffer = new byte[2048];
                    int count = 0;
                    int num;
                    do
                    {
                        num = request.InputStream.Read(buffer, 0, buffer.Length);
                        count += num;
                        byteList.AddRange((IEnumerable<byte>)buffer);
                    }
                    while (num != 0);
                    string query = Encoding.UTF8.GetString(byteList.ToArray(), 0, count);
                    Console.WriteLine("提交數(shù)據(jù):" + query);
                    using (HttpClient httpClient = new HttpClient())
                    {
                        httpClient.BaseAddress = new Uri(str);
                        Dictionary<string, string> dictionary = new Dictionary<string, string>();
                        NameValueCollection queryString = HttpUtility.ParseQueryString(query);
                        foreach (string allKey in queryString.AllKeys)
                            dictionary.Add(allKey, queryString[allKey]);
                        HttpContent content = (HttpContent)new FormUrlEncodedContent((IEnumerable<KeyValuePair<string, string>>)dictionary);
                        string result = httpClient.PostAsync(str, content).Result.Content.ReadAsStringAsync().Result;
                        Console.WriteLine("Body:" + result);
                        return result;
                    }
                }
                else
                {
                    using (HttpClient httpClient = new HttpClient())
                    {
                        httpClient.BaseAddress = new Uri(str);
                        string result = httpClient.GetAsync(str).Result.Content.ReadAsStringAsync().Result;
                        Console.WriteLine("Body:" + result);
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
                response.StatusDescription = "404";
                response.StatusCode = 404;
                return "在接收數(shù)據(jù)時發(fā)生錯誤:" + ex.ToString();
            }
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容