C # HttpWebRequest PHP cURLcode dönüştürmek nasıl?

0 Cevap

Ben herhangi php hiçbir deneyimi var, ama ben bu kod C # eşdeğer oluşturmanız gerekir:

<?
$theData = array(
'action'=>'login',
'data'=>array(
'username'=>'(the username to be used)',
'password'=>'(the password)'
),
);
echo "REQUEST:\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '(the service url)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('query'=>json_encode($theData)));
curl_setopt($ch, CURLOTP_SSL_VERIFYPEER, false);
$login = curl_exec($ch);
#echo $login;
var_dump(json_decode($login, true));
#cho "\n";
curl_close($ch);      

C #.

Şimdi, ben etrafında alınamıyor tek şey bu satırı:

curl_setopt($ch, CURLOPT_POSTFIELDS , array('query'=>json_encode($passedData))); 

From what I gather this is the equivalent of setting the HttpWebRequest's RequestStream. However, what kind of object should I put in the stream and how should I serialize it? I am using the Json.NET library found here: http://json.codeplex.com/ My current C# code is this(it is test code, just to see how it would work):

        StringWriter sw = new StringWriter(new StringBuilder());
        using (JsonWriter jwr = new JsonTextWriter(sw))
        {
            jwr.Formatting = Formatting.Indented;                
            jwr.WriteStartObject();            
            jwr.WritePropertyName("action");
            jwr.WriteValue("login");
            jwr.WritePropertyName("data");
            jwr.WriteStartObject();                
            jwr.WritePropertyName("username");
            jwr.WriteValue((the username));
            jwr.WritePropertyName("password");
            jwr.WriteValue((the password));                  
            jwr.WriteEnd();                
            jwr.WriteEndObject();
        }
        string data = sw.ToString();
        Console.WriteLine(data); //yes, the json string is correct
        //uri of the service
        Uri address = new Uri((the service uri));            
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
        ServicePointManager.ServerCertificateValidationCallback = delegate
        {
            return
                true; //always trust the presented cerificate
        };           
        request.Method = "post";          
        request.ContentType = "application/json";            
        string response = null;
        try
        {
            using (Stream s = request.GetRequestStream())
            {                
                using (StreamWriter stw = new StreamWriter(s))
                {                        
                    stw.Write(data); //obviously this adds just the json object
                                     //and not the array() map, hence the server
                                     //returns an error.                                   
                }
            } 

            using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
            {
                Console.WriteLine();
                var reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
                response = reader.ReadToEnd();
            }
            Console.WriteLine(response);
        }

My guess is that I would have to use Dictionary, but in that case how do I pass it to the Request stream? Thank you in advance.

0 Cevap