// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();
static async Task APITest()
{
// Call asynchronous network methods in a try/catch block to handle exceptions.
try
{
HttpResponseMessage response = await client.GetAsync(****************);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Debug.Log(responseBody);
}
catch(Exception e)
{
Debug.Log("nException Caught!");
Debug.Log("Message :{0} ",e.Message);
}
它只打印错误代码(如404或400),而不打印错误消息。
但是当检查postman时打印错误消息与错误代码一起。
任何见解,都会有所帮助。
我发现这个博客有很好的例子:http://nodogmablog.bryanhogan.net/2016/07/getting-web-api-exception-details-from-a-httpresponsemessage/
也尝试从响应获取消息
一般来说,当你使用Unity时,你不希望有一个Main
方法!Unity已经提供了应用程序框架和生命周期!
通常在Unity中,你不希望使用Console.Write
,而是使用Debug.Log
!
在您的特定情况下,Debug.LogException
,所以只需执行
Debug.LogException(e);
将记录整个异常,包括堆栈跟踪和所有内容到控制台/日志文件。
最后你宁愿用UnityWebRequest.Get
代替。