如何发送参数上的HTTPS POST用C#
-
20-09-2019 - |
题
这里
我也问过 的如何使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);
}
但是服务器总是回答说没有“查询”的参数。任何帮助?
提前感谢!
解决方案
我会使用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);
}
不隶属于 StackOverflow