我正在C#中開發Windows 8(Consumer Preview)的應用程序。它會對Last.fm API進行簡單的REST調用。HttpResponseMessage中的異常在.NET4.5中的異步POST方法
有這樣的例外,這是多年來困擾我的現在。我正在嘗試將POST調用發送給Last.fm API方法。但每當我發送一個電話,我得到以下mssg -
「mscorlib.dll中發生類型'System.Net.Http.HttpRequestException'的異常,但未在用戶代碼中處理」。 附加信息:發送請求時發生錯誤。
如果我打印出來,它說的例外 -
System.Net.Http.HttpRequestException:發送請求時發生錯誤。 ---> System.Net.WebException:底層連接已關閉:連接意外關閉。
在System.Net.HttpWebRequest.EndGeetResponse(IAsyncResult的asyncResult)
在System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult的AR)
我GET調用的最後驗證API。 fm工作正常。
我附上一段代碼:
private async void Collection_Click_1(object sender, RoutedEventArgs e)
{
/* .
.
.
*/
HttpClient Cli = new HttpClient();
string track_updateNowPlaying = "method=track.updateNowPlaying&track=" + id3.Title + "&artist=" +id3.Artist + "&album=" + id3.Album + "&api_key=" + lfm_api_key + "&api_sig=" + updtrack_sig + "&sk=" + Globalv.session_key;
HttpContent tunp =new StringContent(track_updateNowPlaying);
try
{
//getting exception in the following line
HttpResponseMessage upd_now_playing = await cli.PostAsync(new Uri("http://ws.audioscrobbler.com/2.0/", UriKind.RelativeOrAbsolute), tunp);
}
catch(Exception ex) {textblock.text = ex.ToString();}
}
private async void LoginBtn_Click_1(object sender, RoutedEventArgs e) //this function is called before collection_click_1 function
{
/* .
.
.
*/
HttpClient cli = new HttpClient();
string auth_request = "http://ws.audioscrobbler.com/2.0/?method=auth.getMobileSession&username=" + UsernameTBx.Text + "&authToken=" + lfm_authToken + "&api_key=" + lfm_api_key + "&api_sig=" + lfm_api_sig;
HttpResponseMessage auth = await cli.GetAsync(auth_request); //this works fine...
}
請讓我知道,如果一個堆棧跟蹤是必要的。
-Sagar
謝謝!!我一直在努力解決這個問題幾天,我無法在任何地方找到解決方案。你拯救了我的一天。 – 2014-01-30 16:44:16