C# 使用MultipartFormDataContent与其他一些参数发布图像
我需要将一些数据发布到API,但我不确定将标题放在哪里。这是我的密码:C# 使用MultipartFormDataContent与其他一些参数发布图像,c#,json,post,windows-phone,content-type,C#,Json,Post,Windows Phone,Content Type,我需要将一些数据发布到API,但我不确定将标题放在哪里。这是我的密码: private async Task<string> PostTest() { string boundary = "----CustomBoundary" + DateTime.Now.Ticks.ToString("x"); string servResp = ""; strURI = "www.something.." Htt
private async Task<string> PostTest()
{
string boundary = "----CustomBoundary" + DateTime.Now.Ticks.ToString("x");
string servResp = "";
strURI = "www.something.."
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Remove("Content-Type");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
using (var content = new MultipartFormDataContent(boundary))
{
content.Add(new StringContent("application/json"), "Expected-Response-Mime-Type");
content.Add(new StringContent("en-US"), "language");
content.Add(new StringContent("blue"), "color");
content.Add(new StringContent("12/11/15"), "date");
ImageForm.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
content.Add(ImageForm, "myPic", MyFileName);
HttpClientHandler handler = new HttpClientHandler();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, strExpenseURL);
request.Headers.ExpectContinue = false;
request.Content = content;
httpClient = new HttpClient(handler);
HttpResponseMessage response = await httpClient.SendAsync(request);
servResp = await response.Content.ReadAsStringAsync();
}
return servResp;
}
我希望我的请求内容类型为:多部分/表单数据;边界=+边界
我的响应内容类型:application/json
这些是我设置这两个的正确位置吗?试试:
HttpClient client = new HttpClient();
HttpMultipartFormDataContent multipart = new HttpMultipartFormDataContent();
multipart.Add(new HttpStringContent("Bob"), "name");
multipart.Add(new HttpStringContent("Hero"), "job", "foo.txt");
multipart.Add(new HttpBufferContent((new byte[] { 0x21, 0x22, 0x23, 0x24 }).AsBuffer()), "Thing3");
HttpResponseMessage response = await client.PostAsync(uri, multipart);
Debug.WriteLineresponse.Content
阅读更多信息。尝试:
HttpClient client = new HttpClient();
HttpMultipartFormDataContent multipart = new HttpMultipartFormDataContent();
multipart.Add(new HttpStringContent("Bob"), "name");
multipart.Add(new HttpStringContent("Hero"), "job", "foo.txt");
multipart.Add(new HttpBufferContent((new byte[] { 0x21, 0x22, 0x23, 0x24 }).AsBuffer()), "Thing3");
HttpResponseMessage response = await client.PostAsync(uri, multipart);
Debug.WriteLineresponse.Content
阅读更多信息。将StringContent替换为HttpStringContent将StringContent替换为HttpStringContent