【发布时间】:2018-09-04 17:38:46
【问题描述】:
我必须使用多个参数向 Web 服务发送 POST 请求,其中一个参数具有 byte[] 类型。但我不知道如何传递 byte[] 参数。有人知道吗?另外,我想知道如何在 GET 请求中发送 byte[] 数组。任何帮助将不胜感激!
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["thing1"] = "hello";
values["thing2"] = "world"; // how to pass byte[] here?
var response = client.UploadValues("http://www.example.com/recepticle.aspx", values);
var responseString = Encoding.Default.GetString(response);
}
或其他带有 HttpClient 的变体:
private static readonly HttpClient client = new HttpClient();
var values = new Dictionary<string, string>
{
{ "thing1", "hello" },
{ "thing2", "world" } // how to pass byte[] here?
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
【问题讨论】:
-
好吧,如果您使用的结构是字符串值,则不能传递 byte[] 数组...除非您使用 Base 64 对其进行编码。
-
查看stackoverflow.com/a/23547930/3098521,看看它是否能回答您的问题。
标签: c# post get post-parameter