C# 在vbnet或C中发布PHP数组#
我用PHP编写了这段代码C# 在vbnet或C中发布PHP数组#,c#,php,arrays,vb.net,curl,C#,Php,Arrays,Vb.net,Curl,我用PHP编写了这段代码 <?php $key = md5('test123'.date("Y-m-d")."3dgdnk5k1yfw3rpy3r5mi25w2zrtmg"); $fields = array( 'apiID' => 'test123', 'date' => date("Y-m-d") , 'token' => $key, 'collection' => array( array(
<?php
$key = md5('test123'.date("Y-m-d")."3dgdnk5k1yfw3rpy3r5mi25w2zrtmg");
$fields = array(
'apiID' => 'test123',
'date' => date("Y-m-d") ,
'token' => $key,
'collection' => array(
array(
"ID" => 35,
"fecha" => '2013-11-20',
"hora" => '12:15',
"num" => '36646363463636',
"tipo" => 'multa',
"calle" => 'rivadavia',
"altura" => '450',
"dominio" => 'HDP 123'
) ,
array(
"ID" => 36,
"fecha" => '2013-11-20',
"hora" => '12:16' ,
"num" => '36646363463636',
"tipo" => 'hola',
"calle" => 'mitre',
"altura" => '450',
"dominio" => 'HDP 123'
)
)
);
$postFields = http_build_query($fields, 'flags_');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://testpage.com/api");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
$r = curl_exec($curl);
curl_close($curl);
?如何发布这些数组值
提前感谢。我找到了解决方案,请发送此字符串:
collection[0][IDacta]=35&collection[0][fecha]=2013-11-20&collection[0][hora]=12:15&collection[0][num]=36646363463636&collection[0][tipo]=multa&collection[0][calle]=alberdi&collection[0][altura]=450&collection[0][dominio]=HDP 123&collection[1][IDacta]=36&collection[1][fecha]=2013-11-20&collection[1][hora]=10&collection[1][num]=36646363463636&collection[1][tipo]=multa&collection[1][calle]=alberdi&collection[1][altura]=450&collection[1][dominio]=HDP 123
使用C#.net
public static void test(object[] objInput)
{
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("firstName", "test");
data.Add("lastName", "test");
String post_string = "";
foreach (KeyValuePair<string, string> post_value in data)
{
post_string += post_value.Key + "=" + HttpUtility.UrlEncode(post_value.Value) + "&";
}
post_string = post_string.TrimEnd('&');
string VOID_URL = "http://your uri///";
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(VOID_URL);
objRequest.Method = "POST";
objRequest.Accept = "application/json";
objRequest.ContentLength = post_string.Length;
objRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
// post data is sent as a stream
StreamWriter myWriter = null;
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(post_string);
myWriter.Close();
// returned values are returned as a stream, then read into a string
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
post_string = responseStream.ReadToEnd();
responseStream.Close();
}
System.Windows.Forms.MessageBox.Show("Response" + post_string);
}
公共静态无效测试(对象[]objInput)
{
字典数据=新字典();
数据。添加(“名字”、“测试”);
数据。添加(“姓氏”、“测试”);
字符串post_String=“”;
foreach(数据中的KeyValuePair post_值)
{
post_string+=post_value.Key+“=”+HttpUtility.UrlEncode(post_value.value)+“&”;
}
post_string=post_string.TrimEnd('&');
字符串VOID_URL=”http://your uri//”;
HttpWebRequest objRequest=(HttpWebRequest)WebRequest.Create(VOID_URL);
objRequest.Method=“POST”;
objRequest.Accept=“application/json”;
objRequest.ContentLength=post_string.Length;
objRequest.ContentType=“application/x-www-form-urlencoded;charset=utf-8”;
//post数据作为流发送
StreamWriter myWriter=null;
myWriter=newstreamwriter(objRequest.GetRequestStream());
myWriter.Write(post_字符串);
myWriter.Close();
//返回的值作为流返回,然后读入字符串
HttpWebResponse objResponse=(HttpWebResponse)objRequest.GetResponse();
使用(StreamReader responseStream=newstreamreader(objResponse.GetResponseStream())
{
post_string=responseStream.ReadToEnd();
responseStream.Close();
}
System.Windows.Forms.MessageBox.Show(“响应”+post_字符串);
}
数组内部数组,我从来没有见过这种类型的PASTION动态,如果您控制连接的两端,我强烈建议您考虑使用包含JSON的单个表单字段,因为它更容易操作,PHP和.NET支持(DE)序列化任意对象。特别是,看看.Net的Newtonsoft.Json库(默认包含在后面的(.Net 3.0+?)Asp.Net WebsiteShanks!!!但是我没有访问服务器端的权限,它是一个私有api,我只能发出请求,但我不能在服务器端更改任何内容。
public static void test(object[] objInput)
{
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("firstName", "test");
data.Add("lastName", "test");
String post_string = "";
foreach (KeyValuePair<string, string> post_value in data)
{
post_string += post_value.Key + "=" + HttpUtility.UrlEncode(post_value.Value) + "&";
}
post_string = post_string.TrimEnd('&');
string VOID_URL = "http://your uri///";
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(VOID_URL);
objRequest.Method = "POST";
objRequest.Accept = "application/json";
objRequest.ContentLength = post_string.Length;
objRequest.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
// post data is sent as a stream
StreamWriter myWriter = null;
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(post_string);
myWriter.Close();
// returned values are returned as a stream, then read into a string
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
post_string = responseStream.ReadToEnd();
responseStream.Close();
}
System.Windows.Forms.MessageBox.Show("Response" + post_string);
}