如何使用C#在Https POST上发送参数

MaL*_*_eS 5 c# parameters https json http-post

我在这里如何制作https帖子,现在工作正常.现在的问题是如何发送参数,名称查询,这是一个json字符串:

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

我正在做什么和不行的是:

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);                    
}
Run Code Online (Sandbox Code Playgroud)

但服务器总是回答说没有'查询'参数.有帮助吗?

提前致谢!

Mar*_*ell 9

我会用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);
        }
Run Code Online (Sandbox Code Playgroud)