1
2 /// <summary>
3 /// 向服务器发送post请求(修改头像)
4 /// </summary>
5 /// <param name="url">路径</param>
6 /// <returns></returns>
7 public async static Task<string> SendPostRequest(string url)
8 {
9 try
10 {
11 Dictionary<string, object> dic = new Dictionary<string, object>();
12 dic.Add("GWnumber", setHeadPicture.GWnumber);
13 dic.Add("token", setHeadPicture.Token);
14 dic.Add("file", setHeadPicture.File);//file值是StorageFile类型
15 dic.Add("systemType", setHeadPicture.SystemType);
16
17 HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();
18 foreach (KeyValuePair<string, object> item in dic)
19 {
20 if (item.Key == "file")
21 {
22 StorageFile file = item.Value as StorageFile;
23 HttpStreamContent streamContent = new HttpStreamContent(await file.OpenReadAsync());
24 form.Add(streamContent, item.Key, "file.jpg");//注意:这里的值是必须的,图片所以使用的是HttpStreamContent
25 }
26 else
27 {
28 form.Add(new HttpStringContent(item.Value + ""), item.Key);
29 }
30 }
31 HttpClient httpClient = new HttpClient();
32 HttpResponseMessage response = await httpClient.PostAsync(new Uri(url), form).AsTask();
33 var contentType = response.Content.Headers.ContentType;
34 if (string.IsNullOrEmpty(contentType.CharSet))
35 {
36 contentType.CharSet = "utf-8";
37 }
38 return await response.Content.ReadAsStringAsync();
39
40 }
41 catch (Exception ex)
42 {
43 throw ;
44 }
45 }