2

I have this code in 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);

I need the code equivalent to this request in VB NET or C#

the code that I have done so far in VB NET is this

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

¿ How POST these array values ?

Thanks in advance.

2

2 Answers 2

1

I found the solution, send this string:

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
Sign up to request clarification or add additional context in comments.

2 Comments

If you control both ends of the connection, I strongly suggest you consider using a single form field containing json as it's much easier to manipulate and both PHP and .Net support (de)serializing arbitrary objects. In particular, look at the Newtonsoft.Json library for .Net (included by default in later (.Net 3.0+?) Asp.Net websites
thanks !!! but I have no access to the server side, it is a private api, I have access to only make requests, but I can not change anything on the server side.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.