gpt4 book ai didi

c# - 如何手动创建嵌套的 POST 参数? (例如,我正在 .Net 中创建请求以联系 Rails 后端)

转载 作者:可可西里 更新时间:2023-11-01 16:50:59 26 4
gpt4 key购买 nike

如何为 http 网络请求手动创建嵌套的 POST 参数?我有一个 .NET C# 客户端,我正在为其创建对 Rails 页面的 HTTP 请求。到目前为止一切都很好,但是我注意到我为请求创建的参数(键/值对)应该是嵌套的。实际上,我也很难尝试在 Controller before_filter 中解决如何对原始请求内容进行“放置”以查看成功的请求如何格式化它。

RAILS BACKEND EXPECTATION(一个成功的登录文件,当我从浏览器(不是 .net)调用时)

 action_controller.request.request_parameters: !map:HashWithIndifferentAccess
commit: Save
webfile: !map:HashWithIndifferentAccess
path: winter
file: &id005 !ruby/object:File
content_type: image/jpeg
original_path: Winter.jpg

C# 参数创建:

    var form = new NameValueCollection();
form["path"] = "winter"; ==> THIS DOESN'T WORK BECAUSE I THINK IT MAY HAVE TO BE NESTED WITHIN THE "webfile" HASH

C# 例程:

    public static HttpWebResponse Upload(HttpWebRequest req, UploadFile[] files, NameValueCollection form)
{
List<MimePart> mimeParts = new List<MimePart>();

try
{
foreach (string key in form.AllKeys)
{
StringMimePart part = new StringMimePart();

part.Headers["Content-Disposition"] = "form-data; name=\"" + key + "\"";
part.StringData = form[key];

mimeParts.Add(part);
}

int nameIndex = 0;

foreach (UploadFile file in files)
{
StreamMimePart part = new StreamMimePart();

if (string.IsNullOrEmpty(file.FieldName))
file.FieldName = "file" + nameIndex++;

part.Headers["Content-Disposition"] = "form-data; name=\"" + file.FieldName + "\"; filename=\"" + file.FileName + "\"";
part.Headers["Content-Type"] = file.ContentType;

part.SetStream(file.Data);

mimeParts.Add(part);
}

string boundary = "----------" + DateTime.Now.Ticks.ToString("x");

req.ContentType = "multipart/form-data; boundary=" + boundary;
req.Method = "POST";

long contentLength = 0;

byte[] _footer = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");

foreach (MimePart part in mimeParts)
{
contentLength += part.GenerateHeaderFooterData(boundary);
}

req.ContentLength = contentLength + _footer.Length;

byte[] buffer = new byte[8192];
byte[] afterFile = Encoding.UTF8.GetBytes("\r\n");
int read;

using (Stream s = req.GetRequestStream())
{
foreach (MimePart part in mimeParts)
{
s.Write(part.Header, 0, part.Header.Length);

while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
s.Write(buffer, 0, read);

part.Data.Dispose();

s.Write(afterFile, 0, afterFile.Length);
}

s.Write(_footer, 0, _footer.Length);
}

return (HttpWebResponse)req.GetResponse();
}
catch
{
foreach (MimePart part in mimeParts)
if (part.Data != null)
part.Data.Dispose();

throw;
}
}

谢谢

附言。特别是我认为我所追求的是:* Rails 如何将嵌套的表单参数/哈希序列化为实际的 HTTP 请求主体,和/或* 指向执行此操作的 Rails 代码库中特定类的指针(因此我可以查看然后在我的 .net 客户端中模拟)

最佳答案

您并不完全清楚您要在这里做什么。所以我认为我们无法为您解决问题。

我最好的建议是您使用监控请求的工具通过网络提交表单,例如 Firebug .它会准确地告诉您 HTTP 请求中的内容,而不是 Rails 解释的内容。您可以使用该信息来制作您想要的 HTTP 请求。

仅供引用 Rails 在键中使用成对的方括号来表示参数中的嵌套和数组。空方括号表示列表,而实心方括号表示另一层嵌套。具有无差异访问的哈希是一种对用于访问哈希的所有键的字符串和符号进行隐式 to_s 调用的哈希。

例子:

当您创建这样的表单时:

var form = new NameValueCollection();
form["user[name]"] = "EmFi";
form["user[phone_number]" = "555-555-1234";
form["user[friend_ids][]" = "7";
form["user[friend_ids][]" = "8";
form["user[address][street_number]" = "75";
form["user[address][street_name]" = "Any St.";
form["user[address][province]" = "Ontario";
form["user[address][country]" = "Candad";

然后将其传递给问题中发布的子程序,这是 rails 将提供给 Controller 的参数哈希:

params = {
"user" => {
"name" => "EmFi,
"phone_number" => "555-555-1234",
"friend_ids" => ["7","8"],
"address" => {
"street_number" => "75",
"street_name" => "any St.",
"province" => "Ontario",
"country" => "Canada",
}
}
}

关于c# - 如何手动创建嵌套的 POST 参数? (例如,我正在 .Net 中创建请求以联系 Rails 后端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1857252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com