我正在向/ document(swagger)发送帖子,该帖子应该使用正文内容上传文档
{
"Application": "tickets",
"File": "some binary data"
}
后面是使用招摇/文件,所以我相信我的标题没有正确。
问题是我没有得到需要发送的标头的正确组合:
Authorization : xxxx;
Content-Type : multipart/form-data;
Content-Type : image/png;
Content-Type : application/json;
FileName : file_name
响应:
415 Unsupported Media Type
FileInfo fi = new FileInfo(@"file");
byte[] fileContents = File.ReadAllBytes(fi.FullName);
ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents);
byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
byteArrayContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
FileName = fi.Name,
};
MultipartFormDataContent multiPartContent = new MultipartFormDataContent();
multiPartContent.Add(new StringContent("document storage"), "Application");
multiPartContent.Add(byteArrayContent, "File");
string header = string.Format("WRAP access_token=\"{0}\"", "xxxx");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "/document");
request.Headers.Add("Authorization", header);
request.Content = multiPartContent;
HttpClient httpClient = new HttpClient();
try
{
Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
HttpResponseMessage httpResponse = httpRequest.Result;
HttpStatusCode statusCode = httpResponse.StatusCode;
HttpContent responseContent = httpResponse.Content;
if (responseContent != null)
{
Task<String> stringContentsTask = responseContent.ReadAsStringAsync();
String stringContents = stringContentsTask.Result;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}