首页 > 解决方案 > 如何在 C# 中解析 HTTP 获取和发布文本?

问题描述

假设我有以下文本:

string get = "GET /?cssbct HTTP/1.1\r\nHost: locationserver\r\n<optional header lines>\r\n"
string post = "POST HTTP/1.0\r\nHost: locationserver\r\nContent-Length: 72\r\n
<optional header lines>\r\nname=cssbct&location=RB-999";

我想恢复参数 Host、namelocation和它们各自的值:locationservercssbctRB-999.

我怎样才能做到这一点?

我尝试了什么:

static void ReadFromClient(ClientClass client)
{
       try
        {
            Console.WriteLine("New Thread of [{0}] in [ServerProgram.ReadfromClient", client.ID);

            CommandLineParser parser = new CommandLineParser();
            parser.FlagChar = new List<string>(new string[] {"-"});

            while (client.Tcp.Connected)
            {
                string args = client.Read();
                args = args.Replace('\r', ' ');
                args = args.Replace('\n', ' ');

                ///Regex r = new Regex("([^\" ][^ ]*)|(\"[^\"]*\")");
                ///string [] splittedArgs = r.Matches(args).OfType<Match>().Select(m => m.Value).ToArray();
                string[] splittedArgs = Regex.Split(args, "\\s+");


                parser.Args = new List<string>(splittedArgs);
                parser.Acquire();

                if(args.Contains("GET") || args.Contains("PUT") || args.Contains("POST"))
                {
                    List<string> commands = parser.Args;

                    commands[1] = commands[1].TrimStart(new char[] { '/', ' ', '?'});//recover [name]

                    if (!args.Contains("HTTP")) // HTTP 0.9
                    {
                        #region HTTP 0.9
                        if (commands[0] == "GET")//HTTP-0.9 request received
                        {
                            if (NameLocationDictionary.ContainsKey(commands[1]))
                            {
                                string location = NameLocationDictionary[commands[1]];

                                string responseText = Http09Text.GET_name_Response_Success_3(location);
                                client.Write(responseText);

                                client.Disconnect();
                                Console.WriteLine(responseText);
                            }
                            else
                            {
                                string responseText = Http09Text.GET_PUT_name_Response_Fail_4();
                                client.Write(responseText);
                                client.Disconnect();
                                Console.WriteLine(responseText);
                            }
                        }

                        if (commands[0] == "PUT")
                        {
                            if (NameLocationDictionary.ContainsKey(commands[1]))//update [location]
                            {
                                NameLocationDictionary[commands[1]] = commands[2];
                                string responseText = Http09Text.PUT_name_location_Response_Success_5(commands[2]);
                                client.Write(responseText);
                                client.Disconnect();
                                Console.WriteLine(responseText);
                            }
                            else
                            {
                                string responseText = Http09Text.GET_PUT_name_Response_Fail_4();
                                client.Write(responseText);
                                client.Disconnect();
                                Console.WriteLine(responseText);
                            }
                        }
                        #endregion
                    }
                    else // HTTP 1.0, or, HTTP 1.1 ...
                    {
                        if (args.Contains("HTTP/1.0"))
                        {
                            if (commands[0] == "GET")//HTTP-1.0 request received
                            {
                                if (NameLocationDictionary.ContainsKey(commands[1]))
                                {
                                    string location = NameLocationDictionary[commands[1]];

                                    string responseText = Http10Text.GET_name_Response_Success_3(location);
                                    client.Write(responseText);
                                    client.Disconnect();
                                    Console.WriteLine(responseText);
                                }
                                else
                                {
                                    string responseText = Http10Text.GET_PUT_name_Response_Fail_4();
                                    client.Write(responseText);
                                    client.Disconnect();
                                    Console.WriteLine(responseText);
                                }
                            }
                            if (commands[0] == "POST")
                            {
                                List<string> comms = commands;

                                //string [] strs = comms[1].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                                //Regex rr = new Regex("([^\" ][^ ]*)|(\"[^\"]*\")");
                                string[] strs = Regex.Split(comms[1], "\\s+");

                                string key = strs[0];
                                string location = commands[8];

                                if (NameLocationDictionary.ContainsKey(key))//update [location]
                                {
                                    NameLocationDictionary[key] = location;
                                    string responseText = Http10Text.PUT_name_location_Response_Success_5();
                                    client.Write(responseText);
                                    client.Disconnect();
                                    Console.WriteLine(responseText);
                                }
                                else
                                {
                                    string responseText = Http10Text.GET_PUT_name_Response_Fail_4();
                                    client.Write(responseText);
                                    client.Disconnect();
                                    Console.WriteLine(responseText);
                                }
                            }
                        }

                        if (args.Contains("HTTP/1.1"))
                        {
                            if (commands[0] == "GET")//HTTP-1.0 request received
                            {
                                string key = commands[1];

                                if (NameLocationDictionary.ContainsKey(key))
                                {                                        
                                    string location = NameLocationDictionary[key];

                                    string responseText = Http11Text.GET_name_Response_Success_3(location);
                                    client.Write(responseText);
                                    client.Disconnect();
                                    Console.WriteLine(responseText);
                                }
                                else
                                {
                                    string responseText = Http11Text.GET_POST_name_Response_Fail_4();
                                    client.Write(responseText);
                                    client.Disconnect();
                                    Console.WriteLine(responseText);
                                }
                            }

                            if (commands[0] == "POST")
                            {
                                if (NameLocationDictionary.ContainsKey(commands[1]))//update [location]
                                {
                                    NameLocationDictionary[commands[1]] = commands[2];
                                    string responseText = Http11Text.POST_name_location_Response_Success_5();
                                    client.Write(responseText);
                                    client.Disconnect();
                                    Console.WriteLine(responseText);
                                }
                                else
                                {
                                    string responseText = Http11Text.GET_POST_name_Response_Fail_4();
                                    client.Write(responseText);
                                    client.Disconnect();
                                    Console.WriteLine(responseText);
                                }
                            }
                        }
                    }
                }
 }

标签: c#regexhttpparsingtcp

解决方案


推荐阅读