2009-10-08 5 views
5

Ho chiesto here come rendere il post https, e ora funziona bene. Problema ora è come inviare un parametro query di nome, che è una stringa JSON:Come inviare parametri su un Https POST con C#

{ "key1": "value1", "key2": { "Key21": "val21"}}

Cosa che sto facendo e non funziona è:

HttpWebRequest q = (HttpWebRequest)WebRequest.Create(Host + ":" + Port); 
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); 
q.Method = "POST"; 
q.ContentType = "application/json"; 
q.Headers.Add("JSON-Signature", GetFirma(query)); 
q.Credentials = new NetworkCredential(user,pass); 

byte[] buffer = Encoding.UTF8.GetBytes("query=" + query); 

q.ContentLength = buffer.Length; 

using (Stream stream = q.GetRequestStream()) 
{ 
    stream.Write(buffer, 0, buffer.Length);      
} 

Ma il server sempre rispondere dicendo che non c'è alcun parametro 'query'. Qualsiasi aiuto?

Grazie in anticipo!

+0

Non ritiene la server di forse si aspettano un parametro di query nell'URL? come http: // host /? query = xyz – Lucero

+0

Questo è quello che non so, in quanto l'unico esempio fornito dagli sviluppatori di server era su php (codice nella prima domanda) –

+0

Vedere anche: http: //marcgravell.blogspot .com/2009/10/pass-data-simply-learning-from-jquery.html –

risposta

9

userei WebClient.UploadValues:

 using (WebClient client = new WebClient()) 
     { 
      NameValueCollection fields = new NameValueCollection(); 
      fields.Add("query", query); 
      byte[] respBytes = client.UploadValues(url, fields); 
      string resp = client.Encoding.GetString(respBytes); 
     } 
+0

Grazie Mark, ma sto ottenendo la stessa risposta del mio codice. Proverò a chiedere di nuovo agli sviluppatori di server per ulteriori feedback. –

+0

Beh, l'ho provato localmente e funziona benissimo, quindi devono essere presenti alcuni problemi di compatibilità o qualcosa di simile. –

+0

Questo codice non invierà la richiesta due volte? Stai chiamando client.UploadValues ​​(url, fields); due volte. –