我在我的系統中進行外部API調用。如果用戶未登錄,代碼將生成404錯誤。我想識別我的代碼中的錯誤。可能嗎? 錯誤這個樣子的 如何檢查C#中的錯誤類型
我的代碼是這樣的
public string ExecuteGetRequest()
{
try
{
_url = Qparams != null ? Utility.WEBAPI_ENDPOINT + Type + "?" + Qparams : Utility.WEBAPI_ENDPOINT + Type; ;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(_url);
if (!string.IsNullOrEmpty(SessionValues.UserID)&& !string.IsNullOrEmpty(SessionValues.AccessToken))
{
var handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(SessionValues.UserID, SessionValues.AccessToken);
string authInfo = SessionValues.UserID + ":" + SessionValues.AccessToken;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
string username = SessionValues.UserID;
String password = SessionValues.AccessToken;
String encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password));
httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
}
httpWebRequest.Method = "GET";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
{
_result = streamReader.ReadToEnd();
}
}
catch (Exception ex)
{
Log.Add(LogTypes.Error,100, ex.ToString());
}
return _result;
}