美文网首页
.Net网页转发工具

.Net网页转发工具

作者: Aweber | 来源:发表于2019-01-21 21:04 被阅读0次

    用法
    http://localhost:端口号/转发网址

            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("网络蹦了:" + ex.ToString());
                }
            }
    
            private static string HandleRequest(HttpListenerRequest request, HttpListenerResponse response)
            {
                try
                {
                    string str = "http:/" + request.RawUrl;
                    Console.WriteLine("用户发起【" + 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("提交数据:" + 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 "在接收数据时发生错误:" + ex.ToString();
                }
            }
    

    相关文章

      网友评论

          本文标题:.Net网页转发工具

          本文链接:https://www.haomeiwen.com/subject/quvhjqtx.html