簡體   English   中英

C# HTTPClient 未發送請求

[英]C# HTTPClient Not Sending Request

我有一個 WPF 應用程序,我需要從遠程 API 檢索配置數據。 API 已經過測試,並且工作正常,返回了我認為正確的格式。 我已經為響應等創建了一個數據類型,但是當我到達通過 HttpClient 獲取響應的行時,它不會向 API 發送任何請求。

數據類型:-

public class Mylist
    {
        public string ItemName { get; set; }
        public string ItemText { get; set; }
        public string ConfigName { get; set; }
        public string ConfigValue { get; set; }
        public string ConfigType { get; set; }
    }

代碼:-

string licKey = ConfigManager.GetSetting("Lic");
            string Uri = ConfigManager.GetSetting("API");
            string UserAPI = ConfigManager.GetSetting("Config");

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(Uri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var response = await client.GetAsync(UserAPI + "?Licence=" + licKey);
            var data = await response.Content.ReadAsAsync<IEnumerable<Mylist>>();
            int count = 0;
            List<Mylist> nudges = new List<Mylist>((IEnumerable<Nudgelist>)data);
// Do something with the data

該代碼為請求構建正確的 URL ( https://example.com/api/Uri?Licence=licencevalue ),如果手動輸入到瀏覽器中,它會給出如下響應:-

<Mylist>
<ConfigName>cName</ConfigName>
<ConfigType>cType</ConfigType>
<ConfigValue>cValue</ConfigValue>
<ItemName>iName</NudgeName>
<ItemText>iText</NudgeText>
</Mylist>
<Mylist>
...
</Mylist>

當我運行上面的代碼並單步執行時,我們會看到“var response = await client.GetAsync(UserAPI + "?Licence=" + licKey);" 這一行它只是跳過代碼的 rest 並進入下一個調用,沒有引發錯誤或發現任何故障。

I have run logs on the API and we are not seeing the request coming in, if we use an identical model of code for calling another API controller to call Uri2 ( https://example.com/api/Uri2?Licence=licencevalue )它工作正常。

嘗試這個


if (response.IsSuccessStatusCode)
{
var stringData = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<IEnumerable<MyList>>(stringData);
}
else { var statusCode= response.StatusCode);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM