Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Help me to convert the following php into asp.net

PHP
$postfields = array(
    'ael_id' => '53198',
    'state' => '1',
    'comment' => 'All is good',
);


$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, "http://rpal-autpert.qette.esnetwours.com/ws/ael/qualif");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); // méthode POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);


$return = curl_exec($ch);


curl_close($ch);


Thanks in advance
Posted
Updated 10-Sep-15 1:35am
v2
Comments
ZurdoDev 10-Sep-15 7:39am    
Where exactly are you stuck?
sunpop 10-Sep-15 7:46am    
Thank you for your reply

How to convert this postfields array in c#?

$postfields = array(
'ael_id' => '53198',
'state' => '1',
'comment' => 'All is good',
);

Thank u
ZurdoDev 10-Sep-15 7:53am    
You can google array and c# and get tons of examples. However that shouldn't be the real problem. I don't do php but I looked up what curl_setopt was and it doesn't really have an equivalent in ASP.Net. However, this article may help you, http://stackoverflow.com/questions/2313438/asp-net-equivalent-of-this-php-code

you need to create dictionary to make it compatible in c# , so you can keep ael_id , state and comment as key and corresponding values accordingly.

Cheers
 
Share this answer
 
Comments
sunpop 11-Sep-15 6:59am    
After creating dictionary how to post it? Please check the following.

Dictionary<string, string=""> array = new Dictionary<string, string="">();

array.Add("ael_id", "53198");
array.Add("state", "1");
array.Add("comment", "All is good'");

string data = JsonConvert.SerializeObject(array);

var dataToSend = Encoding.UTF8.GetBytes(data);
var req = HttpWebRequest.Create(uri);

req.ContentType = "application/json";
req.ContentLength = dataToSend.Length;
req.Method = "POST";
req.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);

response = req.GetResponse() as HttpWebResponse;

Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);



I got Internal server Error. The problem is in req.GetResponse(). Where am I going wrong?
sunpop 16-Sep-15 10:14am    
The above code is correct & the data which I sent was wrong. Thank you for the solution
XML
Dictionary<string, string > postfields = new Dictionary<string, string >()
  {
    {"ael_id", "53198"},
    {"state", "1"},
    {"comment", "All is good"}
   };


 var wc = new WebClient() { BaseAddress = "http://rpal-autpert.qette.esnetwours.com/ws/ael/qualif" };
   foreach (KeyValuePair<string, string> pair in postfields)
     {
        wc.QueryString.Add(pair.Key, HttpUtility.UrlEncode(pair.Value));

     }
  wc.DownloadString("");
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900