2011-01-17 6 views
2

Ich habe keine Erfahrung in PHP auch immer, aber ich brauche die C# äquivalent diesen Code zu erstellen:Wie konvertiert man PHP cURLcode in C# HttpWebRequest?

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

in C#.

Nun, das einzige, was ich nicht umgehen kann, ist diese Zeile:

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

Von dem, was ich sammeln dies entspricht der RequestStream der HttpWebRequest Einstellung. Welche Art von Objekt sollte ich jedoch in den Stream einfügen und wie sollte ich es serialisieren? Ich verwende die Json.NET Bibliothek finden Sie hier: http://json.codeplex.com/ Mein aktueller Code C# ist dies (es ist Testcode, nur um zu sehen, wie es funktionieren würde):

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

Meine Vermutung ist, dass ich würde Verwenden Sie Dictionary, aber in diesem Fall, wie übermittle ich es an den Stream Request? Vielen Dank im Voraus.

Antwort

9

Ok, soll ich nicht ernst aks Fragen, bevor eine schöne Tasse Kaffee ...

Die Antwort auf meine Frage lautet:

stw.Write("query="+data); 

und die Content-Header diese Einstellung:

request.ContentType = "application/x-www-form-urlencoded"; 

Ja, ich bin dumm.

Verwandte Themen