EXAMPLE of parsing GET and POST
2011-08-23 02:38
218 查看
public Dictionary<String, String> parseRequest(String requestString) { // deal with GET and POST Dictionary<String, String> dic = new Dictionary<string, string>(); String[] tmp = requestString.Split(' '); String head = tmp[0].ToUpper(); if(head.Equals("GET")) { // all request should be in the url, deal with parameters try { String[] reqstr = tmp[1].Split('?'); String[] parameters = reqstr[1].Split('&'); foreach (String param in parameters) { String[] sparam = param.Split('='); dic.Add(sparam[0], sparam[1]); } } catch (Exception ex) { return null; } } else if (head.Equals("POST")) { String[] reqstr = requestString.Split('\n'); // locate Post content line int line = 0; for (int i = 0; i < reqstr.Length; i++) { if (reqstr[i].Length <= 1) { line = i; break; } } // prepare post content try { String postContent = reqstr[i + 1]; String[] parameters = postContent.Split('&'); foreach (String param in parameters) { String[] sparam = param.Split('='); dic.Add(sparam[0], sparam[1]); } } catch (Exception ex) { return null; } } else { // check null if nothing parsed. dic = null; } return dic; } } }
=========================
using System.Collections.Generic;
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, int> d = new Dictionary<string, int>(); d.Add("apple", 1); d.Add("windows", 5); // See if Dictionary contains this string if (d.ContainsKey("apple")) // True { int v = d["apple"]; Console.WriteLine(v); } // See if Dictionary contains this string if (d.ContainsKey("acorn")) { Console.WriteLine(false); // Nope } } }
相关文章推荐
- An Example of Pre-Query and Post-Query Triggers in Oracle Forms With Using Display_Item to Highlight Dynamically
- The fundamental differences between "GET" and "POST"
- AJAX GetAndPost请求的完整封装
- <Ejb3InAction> Sample02 for Weblogic - Simple example of Stateless, Stateful, MDB and JPA Enity
- Get vertical scrollbar width and example
- How to get control of your time and your life
- Ground loop problems and how to get rid of them
- Just a record of moss company and example
- Spring @PostConstruct and @PreDestroy example
- Photoshop CS2 RAW : Using Adobe Camera Raw, Bridge, and Photoshop to Get the Most out of Your Digita
- [API]common http get and post
- curl get and post sample(转)
- How to use GET and POST methods in HTTP from a MIDlet
- Example of Using Google Maps Service and GPS in Android
- An example of Integrating Spark and Cassandra
- Andrdoi 如何获得某个月份最后一天(或号数最大那天)的号数?--使用getActualMaximum(Calendar.DAY_OF_MONTH)!
- jenkins groovy postbuild print and example 打印消息和log收集例子
- XML Web Service初体验: HTTP-GET, HTTP-POST and SOAP的比较
- Android HttpClientConnection Get and Post
- whether the results are the same of getClass().getName() called in child class and parent class?