【发布时间】:2018-11-27 17:15:54
【问题描述】:
我正在尝试通过 API REST 将文档(任何文件类型)发布到 GLPI 服务器。
这是我正在做的事情:
private void button11_Click(object sender, EventArgs e)
{
using (var client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
var rcontent = string.Empty;
// HEADERS (URL + Access Tokens)
//string _ContentType = "multipart/form-data";
string _Uri = Properties.Settings.Default.GLPI_URL + "/Document/";
client.BaseAddress = new Uri(_Uri);
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(_ContentType));
client.DefaultRequestHeaders.Add("Session-Token", Properties.Settings.Default.GLPI_SESSION_TOKEN);
client.DefaultRequestHeaders.Add("App-Token", Properties.Settings.Default.GLPI_APP_TOKEN);
// JSON Content (input string array with file uploaded informations)
JSON_C.DocumentAdder JSONContent = new JSON_C.DocumentAdder();
JSONContent.name = "sth";
JSONContent._filename = filebytes;
HttpContent _JSONContent = new StringContent("uploadManifest={\"input\": " + JsonConvert.SerializeObject(JSONContent).ToString() + "}", Encoding.UTF8, "application/json");
content.Add(_JSONContent);
// File Content in bytes
var fileContent = new ByteArrayContent(filebytes);
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("_filename") { FileName = filepath };
//fileContent.ReadAsByteArrayAsync();
content.Add(fileContent);
// Request
HttpResponseMessage reponse;
var _Method = new HttpMethod("POST");
reponse = client.PostAsync(_Uri, content).Result;
// Request response
rcontent = reponse.Content.ReadAsStringAsync().Result;
textBox2.Text = reponse.ToString() + Environment.NewLine + rcontent.ToString();
}
}
}
但这是我得到的回应:
StatusCode:400,ReasonPhrase:“错误请求”,版本:1.1,内容:System.Net.Http.StreamContent,标头:
{
连接:关闭
缓存控制:无存储、必须重新验证、无缓存
日期:格林威治标准时间 2018 年 11 月 26 日星期一 12:50:09
服务器:Apache/2.4.29
服务器:(Ubuntu)
内容长度:61
内容类型:应用程序/json;字符集=UTF-8
过期时间:1997 年 7 月 26 日星期一 05:00:00 GM
}
与:
["ERROR_UPLOAD_FILE_TOO_BIG_POST_MAX_SIZE","文件似乎太大"]
我要上传的文件是 592 字节!一个请求的最大总限制为 2Mo。而 php.ini 中的post_max_size 是“8M”,我将其更改为“0”后的结果相同(完全没有限制)。然后将其设置为 20M 以匹配 upload_max_filesize (/etc/php/7.2/apache2/php.ini)。upload_max_filesize_.. 也是“20M”
【问题讨论】:
标签: c# rest api file-upload