VBNET或C#中的POST PHP数组


POST PHP Arrays in VB NET or C#

我在PHP 中有这段代码

<?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);

我需要VBNET或C#中与此请求等效的代码

到目前为止,我在VBNET中完成的代码是这个

Imports System.Net
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Web
Imports System.Security.Cryptography
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim testString As String = "test1232013-11-223dgdnk5k1yfw3rpy3r5mi25w2zrtmg"
            Dim asciiBytes As Byte() = ASCIIEncoding.ASCII.GetBytes(testString)
            Dim hashedBytes As Byte() = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes)
            Dim hashedString As String = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower()
            Dim post_values As New Dictionary(Of String, String)
            post_values.Add("apiID", "test123")
            post_values.Add("date", "2013-11-22")
            post_values.Add("token", hashedString) ' The hash MD5
            'Here i dont know how to publish the array
            post_values.Add("collection", "IDacta=35&fecha=2013-11-20&hora=12:15&num=36646363463636&tipo=multa&calle=alberdi&altura=450&dominio=HDP 123")
            Dim post_string As String = ""
            For Each post_value As KeyValuePair(Of String, String) In post_values
                post_string += post_value.Key + "=" + HttpUtility.UrlEncode(post_value.Value) + "&"
            Next
            post_string = post_string.TrimEnd("&")
            Dim vSolicitud As WebRequest = WebRequest.Create("http://testpage.com/api")
            vSolicitud.Proxy = Nothing
            vSolicitud.Method = "POST"
            vSolicitud.ContentType = "application/x-www-form-urlencoded"
            Dim vXML As String = post_string
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(vXML)
            vSolicitud.ContentLength = byteArray.Length
            Dim dataStream As Stream = vSolicitud.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()
            Dim vRespuesta As WebResponse = vSolicitud.GetResponse()
            Dim vStream As StreamReader = New StreamReader(vRespuesta.GetResponseStream())
            Dim vStr As String = vStream.ReadToEnd().Trim
            vRespuesta.Close()
            vStream.Close()
            TextBox1.Text = vStr
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

?如何POST这些数组值?

提前谢谢。

我找到了解决方案,发送以下字符串:

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);
    }