我正在爲Windows Phone 8.1編寫一個C#應用程序。應用程序向帶有發佈數據的網頁發送http請求,但是,當我將請求內容複製到字符串時,它會引發異常。HTTP響應中的無效字符集
下面是代碼,
var values = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("username", "usernameHere"),
new KeyValuePair<string, string>("password", "passwordHere"),
};
var httpClient = new HttpClient(new HttpClientHandler());
var response = await httpClient.PostAsync(new Uri(LOGIN_URL), new FormUrlEncodedContent(values));
if (response.IsSuccessStatusCode)
{
string responseString = "";
try
{
responseString = await response.Content.ReadAsStringAsync();
}
catch (Exception e)
{
MessageBox.Show("Exception caught " + e);
}
}
的錯誤是,
「System.InvalidOperationException:字符集 ContentType的規定是無效的使用無效 無法讀取內容爲字符串。字符集---> System.ArgumentException:'ISO8859_1'不是 支持的編碼名稱。「
顯然,解決方案是使用,而不是PostAsync(How to change the encoding of the HttpClient response)GetByteArrayAsync,但這種方式我不能提交後的數據。