PHP的C#等效http_build_query [英] C# Equivalent of PHP http_build_query
问题描述
我需要从我的C#客户端使用的HttpWebRequest
一些数据传递给PHP页面的服务器上。根据文档的预期数据是一个数组的数组,像这样:
I need to pass some data to a PHP page on a server from my C# client using HttpWebRequest
. The expected data according to the documentation is an array of arrays, something like this:
$postData = array(
'label1' => 'myLabel',
'label2' => array(
'label2_1' => 3
'label2_2' => array(
'label2_2_1' => 3
)
)
);
的结构以上仅仅是一个例子。它可以是非常复杂和结构本身不是恒定的。
The structure above is just an example. It can be very complicated and the structure itself is not constant.
在PHP中有一个名为函数 http_build_query
这这些序列化嵌套的PHP数组来一个简单的字符串,它可以作为一个HTTP POST请求的数据发送。问题是我需要从我的C#应用程序调用这个PHP页面。我谨代表这些嵌套阵列既可以作为嵌套的词典<字符串对象>
S,或匿名类型
In PHP there is a function named http_build_query
which serializes these PHP nested arrays to a simple string, which can be sent as the data of a HTTP POST request. The problem is I need to call this PHP page from my C# application. I would like to represent these nested arrays either as nested Dictionary<string, object>
s, or anonymous types.
我怎样才能做到这一点?什么规则呢 http_build_query
跟着来产生输出字符串?
How can I do that? What rules does http_build_query
follow to produce its output string?
有一个非常类似的问题的Converting PHP数组的数组到C#,不解决我的问题,很遗憾的。该接受的答案建议固定结构的解决方案,第二个行不通的。
There is a very similar question Converting PHP array of arrays to C#, which does not solve my problem, unfortunately. The accepted answer recommends a solution for a fixed structure, the second one does not work at all.
推荐答案
那么,有没有按'不像是会是任何内置到.NET,让你做到这一点。不过,如果你想重新实现.NET中的PHP行为,你可以白盒通过在PHP源代码考虑看看实现它,或暗箱通过读取的 http_build_query 和测试功能进行各种输入的PHP文件。
Well, there doesn't seem to be anything built-in to .NET to allow you to do this. However, if you want to re-implement the PHP behavior in .NET, you can either white-box implement it by taking a look at the PHP source code, or black-box implement it by reading the PHP documentation of http_build_query and testing the function out on various inputs.
我拿了一个黑盒方法,并建立了以下类:
I took a black-box approach and created the following class:
/// <summary>
/// Helps up build a query string by converting an object into a set of named-values and making a
/// query string out of it.
/// </summary>
public class QueryStringBuilder
{
private readonly List<KeyValuePair<string, object>> _keyValuePairs
= new List<KeyValuePair<string, object>>();
/// <summary> Builds the query string from the given instance. </summary>
public static string BuildQueryString(object queryData, string argSeperator = "&")
{
var encoder = new QueryStringBuilder();
encoder.AddEntry(null, queryData, allowObjects: true);
return encoder.GetUriString(argSeperator);
}
/// <summary>
/// Convert the key-value pairs that we've collected into an actual query string.
/// </summary>
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}";
}));
}
/// <summary> Adds a single entry to the collection. </summary>
/// <param name="prefix"> The prefix to use when generating the key of the entry. Can be null. </param>
/// <param name="instance"> 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. </param>
/// <param name="allowObjects"> 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. </param>
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<string, object>(prefix, instance));
}
}
/// <summary> Adds the given collection of entries. </summary>
private void Add(string prefix, IEnumerable<Entry> 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;
}
/// <summary>
/// Returns a collection of entries that represent the properties on the object.
/// </summary>
private IEnumerable<Entry> GetObjectAdapter(object data)
{
var properties = data.GetType().GetProperties();
foreach (var property in properties)
{
yield return new Entry()
{
Key = property.Name,
Value = property.GetValue(data)
};
}
}
/// <summary>
/// Returns a collection of entries that represent items in the collection.
/// </summary>
private IEnumerable<Entry> GetArrayAdapter(ICollection collection)
{
int i = 0;
foreach (var item in collection)
{
yield return new Entry()
{
Key = i.ToString(),
Value = item,
};
i++;
}
}
/// <summary>
/// Returns a collection of entries that represent items in the dictionary.
/// </summary>
private IEnumerable<Entry> GetDictionaryAdapter(IDictionary collection)
{
foreach (DictionaryEntry item in collection)
{
yield return new Entry()
{
Key = item.Key.ToString(),
Value = item.Value,
};
}
}
}
该代码是相当自我说明,但是它接受一个字典,数组或一个对象。如果它是一个顶级对象,它序列化的属性。如果它是一个数组,每个元素是序列化与适当的数组索引。如果它是一个字典,键/值序列化。 。数组和包含其他数组或字典值将被夷为平地,类似PHP的行为。
The code is pretty self-explanatory, but it accepts a dictionary, an array, or an object. If it's an top-level object, it serialized the properties. If it's an array, each element is serialized with the appropriate array index. If it's a dictionary, the key/values are serialized. Arrays and Dictionary-values that contain other arrays or dictionaries are flattened, similar to PHPs behavior.
例如,以下内容:
QueryStringBuilder.BuildQueryString(new
{
Age = 19,
Name = "John&Doe",
Values = new object[]
{
1,
2,
new Dictionary<string, string>()
{
{ "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<string, string>()
{
{ "key1", "value1" },
{ "key2", "value2" },
}
}
);
生成:
Generates:
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
这篇关于PHP的C#等效http_build_query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!