HttpClient 呼び出しを使用して簡略化するために作成したメソッドがあります。メソッド HttpReponse.Content.ReadAsAsync().Result を使用して、API からの応答を取得します。
これはすべてうまくいきます。私の方法は次のようになります。
public static T ExecuteAPIGetRequest<T>(string url, Dictionary<string, string> parameters)
{
HttpClient client = new HttpClient();
//basic authentication
var t = new object();
string baseURL = "myurl";
//Execute request
HttpResponseMessage response = client.GetAsync(baseURL).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<T>().Result;
}
else
{
return (T)t;
}
}
私の質問は、クエリが失敗した場合、空の型の T を返す必要があるということです。これは、私が作成したカスタム クラスであれば問題ありませんが、string や string[] などのオブジェクトでは機能しません。何か案は?
乾杯
NCBL