Csharp/C#教程:C#等效的PHP http_build_query分享


C#等效的PHP http_build_query

我需要使用HttpWebRequest从我的C#客户端将一些数据传递到服务器上的PHP页面。 根据文档的预期数据是一个数组数组,如下所示:

 $postData = array( 'label1' => 'myLabel', 'label2' => array( 'label2_1' => 3 'label2_2' => array( 'label2_2_1' => 3 ) ) ); 

上面的结构只是一个例子。 它可能非常复杂,结构本身也不是一成不变的。

在PHP中有一个名为http_build_query的函数,它将这些PHP嵌套数组序列化为一个简单的字符串,该字符串可以作为HTTP POST请求的数据发送。 问题是我需要从我的C#应用​​程序调用这个PHP页面。 我想将这些嵌套数组表示为嵌套的Dictionary或匿名类型。

我怎样才能做到这一点? http_build_query遵循什么规则来生成其输出字符串?

有一个非常类似的问题将PHP数组转换为C# ,遗憾的是这并没有解决我的问题。 接受的答案建议采用固定结构的解决方案,第二种方法根本不起作用。

好吧,.NET似乎没有任何内置function允许你这样做。 但是,如果你想在.NET中重新实现PHP行为,你可以通过查看PHP源代码来实现它,或者通过阅读http_build_query的PHP文档并测试它来实现它。在各种输入上运行。

我采用了黑盒方法并创建了以下类:

 ///  /// Helps up build a query string by converting an object into a set of named-values and making a /// query string out of it. ///  public class QueryStringBuilder { private readonly List> _keyValuePairs = new List>(); ///  Builds the query string from the given instance.  public static string BuildQueryString(object queryData, string argSeperator = "&") { var encoder = new QueryStringBuilder(); encoder.AddEntry(null, queryData, allowObjects: true); return encoder.GetUriString(argSeperator); } ///  /// Convert the key-value pairs that we've collected into an actual query string. ///  private string GetUriString(string argSeperator) { return String.Join(argSeperator, _keyValuePairs.Select(kvp => { var key = Uri.EscapeDataString(kvp.Key); var value = Uri.EscapeDataString(kvp.Value.ToString()); return $"{key}={value}"; })); } ///  Adds a single entry to the collection.  ///  The prefix to use when generating the key of the entry. Can be null.  ///  The instance to add. /// /// - If the instance is a dictionary, the entries determine the key and values. /// - If the instance is a collection, the keys will be the index of the entries, and the value /// will be each item in the collection. /// - If allowObjects is true, then the object's properties' names will be the keys, and the /// values of the properties will be the values. /// - Otherwise the instance is added with the given prefix to the collection of items.  ///  true to add the properties of the given instance (if the object is /// not a collection or dictionary), false to add the object as a key-value pair.  private void AddEntry(string prefix, object instance, bool allowObjects) { var dictionary = instance as IDictionary; var collection = instance as ICollection; if (dictionary != null) { Add(prefix, GetDictionaryAdapter(dictionary)); } else if (collection != null) { Add(prefix, GetArrayAdapter(collection)); } else if (allowObjects) { Add(prefix, GetObjectAdapter(instance)); } else { _keyValuePairs.Add(new KeyValuePair(prefix, instance)); } } ///  Adds the given collection of entries.  private void Add(string prefix, IEnumerable datas) { foreach (var item in datas) { var newPrefix = String.IsNullOrEmpty(prefix) ? item.Key : $"{prefix}[{item.Key}]"; AddEntry(newPrefix, item.Value, allowObjects: false); } } private struct Entry { public string Key; public object Value; } ///  /// Returns a collection of entries that represent the properties on the object. ///  private IEnumerable GetObjectAdapter(object data) { var properties = data.GetType().GetProperties(); foreach (var property in properties) { yield return new Entry() { Key = property.Name, Value = property.GetValue(data) }; } } ///  /// Returns a collection of entries that represent items in the collection. ///  private IEnumerable GetArrayAdapter(ICollection collection) { int i = 0; foreach (var item in collection) { yield return new Entry() { Key = i.ToString(), Value = item, }; i++; } } ///  /// Returns a collection of entries that represent items in the dictionary. ///  private IEnumerable GetDictionaryAdapter(IDictionary collection) { foreach (DictionaryEntry item in collection) { yield return new Entry() { Key = item.Key.ToString(), Value = item.Value, }; } } } 

代码非常明显,但它接受字典,数组或对象。 如果它是顶级对象,则序列化属性。 如果它是一个数组,则每个元素都使用适当的数组索引进行序列化。 如果它是字典,则键/值被序列化。 包含其他数组或字典的数组和字典值被展平,类似于PHP的行为。

例如,以下内容:

 QueryStringBuilder.BuildQueryString(new { Age = 19, Name = "John&Doe", Values = new object[] { 1, 2, new Dictionary() { { "key1", "value1" }, { "key2", "value2" }, } }, }); // 0=1&1=2&2%5B0%5D=one&2%5B1%5D=two&2%5B2%5D=three&3%5Bkey1%5D=value1&3%5Bkey2%5D=value2 QueryStringBuilder.BuildQueryString(new object[] { 1, 2, new object[] { "one", "two", "three" }, new Dictionary() { { "key1", "value1" }, { "key2", "value2" }, } } ); 

产生:

 Age=19&Name=John%26Doe&Values%5B0%5D=1&Values%5B1%5D=2&Values%5B2%5D%5Bkey1%5D=value1&Values%5B2%5D%5Bkey2%5D=value2 

这是:

 Age=19&Name=John%26Doe&Values[0]=1&Values[1]=2&Values[2][key1]=value1&Values[2][key2]=value2 Age=19 Name=John&Doe Values[0]=1 Values[1]=2 Values[2][key1]=value1 Values[2][key2]=value2 

使用NameValueCollection,您可以这样做:

上述就是C#学习教程:C#等效的PHP http_build_query分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)

 private string ToQueryString(NameValueCollection queryData) { var array = (from key in queryData.AllKeys from value in queryData.GetValues(key) select string.Format(CultureInfo.InvariantCulture, "{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value))) .ToArray(); return "?" + string.Join("&", array); } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ctvol.com/cdevelopment/1011822.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐