C#のでHTTPS POSTにパラメータを送信する方法
-
20-09-2019 - |
質問
私はここのを求めている今、HTTPSのポストを作成する方法をを、そしてそれが正常に動作します。問題は今、パラメータ、名前クエリを送信する方法で、ウィッヒは、JSON文字列です。
{ "KEY1": "値1"、 "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);
}
しかし、サーバーは常にNO「クエリ」パラメータがありませんと言って答えます。すべてのヘルプ?
事前に感謝します!
解決
私は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