如何使用ParseQueryString将数组作为查询字符串提交

时间:2015-10-10 09:05:12

标签: c# php arrays

这是我的C#代码

var UriBuilder = new UriBuilder("http://smsgateway.me/api/v3/messages/send/");
var parameters = HttpUtility.ParseQueryString(string.Empty);
parameters["email"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:email", NamespaceManager).Value;
parameters["password"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:password", NamespaceManager).Value;
parameters["device"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:device", NamespaceManager).Value; ;
parameters["number"] = "123456789";//any number
parameters["message"] = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:SMS_TO_BE_SENT", NamespaceManager).Value; ;
UriBuilder.Query = parameters.ToString();
//UriBuilder.Fragment = "some_fragment";

Uri finalUrl = UriBuilder.Uri;
var request = WebRequest.Create(finalUrl);

// Get the response.
WebResponse result = request.GetResponse();

这是示例PHP代码

<?php
include "smsGateway.php";
$smsGateway = new SmsGateway('demo@smsgateway.me', 'password');

$deviceID = 1;
$numbers = ['+44771232343', '+44771232344'];
$message = 'Hello World!';

$options = [
'send_at' => strtotime('+10 minutes'), // Send the message in 10 minutes
'expires_at' => strtotime('+1 hour') // Cancel the message in 1 hour if the message is not yet sent
];

//Please note options is no required and can be left out
$result = $smsGateway->sendMessageToManyNumbers($number, $message, $deviceID, $options);
?>

PHP示例中的数字是一个数组。如何使用HttpUtility.ParseQueryString

在查询字符串中传递数组

1 个答案:

答案 0 :(得分:2)

您可以在查询字符串中添加一个数字数组,如下所示:

parameters["number[0]"] = "123456789";
parameters["number[1]"] = "987654321";

我用这个链接测试了它

http://smsgateway.me/api/v3/messages/send?email=xxx&password=xxx&device=xxx&number[0]=123456789&number[1]=987654321&message=test

并且有效。

修改 根据你的评论。

我不熟悉InfoPath,但基于this documentation,您可以在查询中添加多个数字,如下所示:

XPathNavigator root = MainDataSource.CreateNavigator();
XPathNodeIterator nodes = root.Select("/my:myFields/my:group1/my:myGroup/my:myNumber", NamespaceManager);
var i = 0;
while (nodes.MoveNext())
{
    parameters["number[" + i + "]"] = nodes.Current.Value;
    i++;
}

但要小心,当URI变得非常长时,您可能会遇到问题。