// 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)而不是錯誤訊息。
但是在郵遞員中簽入時會列印錯誤訊息以及錯誤代碼。
任何見解,都會有所幫助。
uj5u.com熱心網友回復:
通常,在使用 Unity 時,您不想要Main方法!Unity 已經提供了應用程式框架和生命周期!
然后通常在 Unity 中,您不想使用Console.Write而是Debug.Log!
在您的具體情況下Debug.LogException,只需這樣做
Debug.LogException(e);
它將記錄整個例外,包括堆疊跟蹤和所有內容到控制臺/日志檔案。
最后你寧愿使用它UnityWebRequest.Get。
uj5u.com熱心網友回復:
我發現這個博客有很好的例子:http : //nodogmablog.bryanhogan.net/2016/07/getting-web-api-exception-details-from-a-httpresponsemessage/
還嘗試從response的屬性中獲取訊息,而不是從例外中獲取。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/329293.html
標籤:C# 统一3d 视觉工作室-2019 http客户端
