【发布时间】:2015-04-10 01:28:39
【问题描述】:
我想要实现的是将PHP复杂对象发送到PHP,目前使用wsd-data但只从根发送属性值,即:
public class Post : Collection<Post>
{
public string Title { get; set; }
public string Content { get; set; }
public File Image { get; set; }
}
byte[] fileContents = ....;
Post post = new Post();
post.Title = "Post title";
post.Content = "Post content";
post.Image = new File ("FileName.png", "image/png", fileContents);
await post.Save();
在这种情况下工作得很好,因为它在内部处理 File 情况,但是如果我添加一个嵌套依赖项,比如
public class Post : Collection<Post>
{
public string Title { get; set; }
public string Content { get; set; }
public File Image { get; set; }
public Author Author { get; set; }
}
假设 Author 是一个具有名称、id 等的类,但是当我发布它时只发送 Author.toString() 值,我尝试添加一个类似键的数组来发布到 PHP,例如:
MultipartFormDataContent form = new MultipartFormDataContent ();
form.Add (new StringContent (post.Author.Name), "Author[Name]");
form.Add (new StringContent (post.Author.Id), "Author[Id]");
await httpClient.PostAsync (url, form).ConfigureAwait (false);
然后在 PHP 中我想收到这样的东西:
<?php
echo $_POST['Author']['Name']; // must print the author name
?>
但我只是得到一个空的 $_POST['Author'] 变量,不知道如何用 c# 实现,如果我需要在内部更改如何创建表单主体,请告诉我,但想使用 form-data因为它支持文件提交。
问候
【问题讨论】:
标签: c# php xamarin xamarin.ios dotnet-httpclient