Pergunta

eu perguntei aqui Como fazer o HTTPS post, e agora isso funciona bem. O problema agora é como enviar um parâmetro, consulta de nome, que é uma string json:

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

O que estou fazendo e não funciona é:

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

Mas o servidor sempre responde dizendo que não há parâmetro 'consulta'. Qualquer ajuda?

Desde já, obrigado!

Foi útil?

Solução

eu usaria 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);
        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top