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

标签 c# json parameters https http-post

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

关于c# - 如何使用 C# 在 Https POST 上发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1537167/

相关文章:

c# - visual studio 2015 发布输出 setup.exe 有错误

javascript - 在 Node.js 中从 JSON 导入数据库中的数据

javascript - 通过 Prop 传递功能后 react 和检查条件

jQuery SerializeArray() JSON 字符串

parameters - 输入、输出、输入、返回 UML 中的参数方向

asp.net - 这是防 Sql 注入(inject)的 Asp.net 代码吗?

php - 如何将 URL 参数列表字符串分解为成对的 [key] => [value] 数组?

c# - ASP.NET 标识 : get all users in a role

c# - 将 Xml 元素添加到文档时出现命名空间问题

C#如何获取正在设置的属性的名称