var client = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Post, "http://url"); // 添加请求内容 request.Content = new StringContent("userId", Encoding.UTF8, "application/json"); var content = new MultipartFormDataContent(); content.Add(new StringContent("54"), "userId"); content.Add(new StreamContent(File.OpenRead("Assets/111.jpg")), "photos", "Assets/111.jpg"); request.Content = content; var response = await client.SendAsync(request); response.EnsureSuccessStatusCode();用UnityC#如何得到返回信息?
时间: 2024-01-07 17:05:46 浏览: 337
可以通过 HttpResponseMessage 对象的 Content 属性获取返回的内容,然后将其转换为需要的数据类型。示例代码如下所示:
```
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "http://url");
request.Content = new StringContent("userId", Encoding.UTF8, "application/json");
var content = new MultipartFormDataContent();
content.Add(new StringContent("54"), "userId");
content.Add(new StreamContent(File.OpenRead("Assets/111.jpg")), "photos", "Assets/111.jpg");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseContent = await response.Content.ReadAsStringAsync(); // 获取返回的内容
Debug.Log(responseContent); // 打印返回的内容
```
在上面的代码中,使用 HttpResponseMessage 对象的 Content 属性获取了返回的内容,并将其转换为字符串类型,最后使用 Debug.Log() 方法将其打印出来。你也可以根据需要将其转换为其他数据类型。
阅读全文