5

我在这里问过如何制作 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);                    
}

但服务器总是回答说没有“查询”参数。有什么帮助吗?

4

1 回答 1

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);
        }
于 2009-10-08T11:22:57.457 回答