由于这种简单的测试有效(在体内传递一个字符串):
服务器代码:
public string PostArgsAndFile([FromBody] string value, string serialNum, string siteNum)
{
string s = string.Format("{0}-{1}-{2}", value, serialNum, siteNum);
return s;
}
客户代码:
private void ProcessRESTPostFileData(string uri)
{
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
var data = "=Short test...";
var result = client.UploadString(uri, "POST", data);
MessageBox.Show(result);
}
}
...然后我试图更进一步(朝着我真正需要做的事情 - 发送文件,而不是字符串);我将服务器代码更改为:
public string PostArgsAndFile([FromBody] byte[] value, string serialNum, string siteNum)
{
byte[] bite = value;
string s = string.Format("{0}{1}{2}", value.ToString(), serialNum, siteNum);
return s;
}
...以及客户端代码:
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
var result = client.UploadFile(uri, @"C:\MiscInWindows7\SampleXLS.csv");
}
...但是在运行时死于:
System.Net.WebException未处理 的HResult = -2146233079 消息=远程服务器返回错误:(415)不支持的媒体类型。
那么如何才能收到以这种方式上传的文件?我需要什么媒体类型,如何指定它?
由于我需要最终发送文件,或者至少发送文件内容,我发现this SO Question/Answer并将我的代码更改为:
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(@"C:\MiscInWindows7\SampleXLS.csv"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
}
string allLines = sb.ToString();
byte[] byteData = UTF8Encoding.UTF8.GetBytes(allLines);
byte[] responseArray = client.UploadData(uri, byteData);
// Decode and display the response.
MessageBox.Show("\nResponse Received.The contents of the file uploaded are:\n{0}",
System.Text.Encoding.ASCII.GetString(responseArray));
}
...虽然这表明我正确地分配了byteData,但似乎没有任何东西进入服务器 - 服务器的“value”属性:
public void PostArgsAndFile([FromBody] byte[] value, string serialNum, string siteNum)
调用方法时,...只是一个空字节数组。
我尝试删除[FromBody],无效(没有更改),并将其更改为[FromUri](我认为不会起作用,但在云雀上尝试过它)导致它因WebException而崩溃
那么如何让服务器接受/识别/接收字节数组?
答案 0 :(得分:0)
这有效:
它本身没有传递文件,或字面意思,但它 传递文件的内容,这对我来说已经足够了。如果客户端有来创建一个xml文件,它只需要在发送之前将该文件序列化为一个字符串,如下所示:
客户端
private void button1_Click(object sender, EventArgs e)
{
ProcessRESTPostXMLFileAsStr("http://Platypus:28642/api/Platypi/PostArgsAndXMLFileAsStr?serialNum=192837465&siteNum=42");
}
private void ProcessRESTPostXMLFileAsStr(string uri)
{
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(@"C:\Platypus\eXtraneousMothLepidoptera.XML"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
}
// I don't know why the prepended equals sign is necessary, but it is
string allLines = "=" + sb.ToString();
var result = client.UploadString(uri, "POST", allLines);
MessageBox.Show(result);
}
}
SERVER(Web API应用程序)
[Route("api/Platypi/PostArgsAndXMLFileAsStr")]
public void PostArgsAndXMLFileAsStr([FromBody] string stringifiedXML, string serialNum, string siteNum)
{
string s = string.Format("{0}{1}{2}", stringifiedXML, serialNum, siteNum);
// Parsing the contents of the stringified XML left as an exercise to the exorcist
}