我正在尝试通过Http Post将对象从一个应用程序流式传输到另一个应用程序,目前我可以将数据从客户端传输到服务器,但是后来我无法解决如何从接收到的数据中获取对象。
我尝试在流中发送字符串和字节,并且在接收端尝试使用二进制格式化程序对数据进行反序列化,将其转换为字节数组或字符串,并修改二进制格式化程序上的Binder以调整程序集和适当地分配对象的类型。
//Client Side:
private static void HttpClientUpload()
{
var baseUrl = "http://myUrl.com/Upload";
var obj = new ComplexTest
{
Name = "test"
};
HttpClient client = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
HttpContent content = new StringContent("upload");
form.Add(content, "upload");
var bytes = ObjectToByteArray(obj);
var stream = new MemoryStream(bytes);
content = new StreamContent(stream);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "upload"
};
form.Add(content);
HttpResponseMessage response = null;
try
{
response = client.PostAsync(baseUrl, form).Result;
}
catch (Exception ex)
{
return;
}
var k = response.Content.ReadAsStringAsync().Result;
}
//Server Side:
public async Task<HttpResponseMessage> Upload()
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
return Request.CreateResponse(HttpStatusCode.InternalServerError);
var request = HttpContext.Current.Request;
var items = request.Form.GetValues("upload");
var streamedString = items[1]; //this now contains my data, but as some sort of stream-based string
}
此操作的结果需要在服务器上安装我的ComplexTest对象,但是目前我所拥有的只是该对象的编译版本,而我不知道该如何处理。