Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET4.5中异步POST方法的HttpResponseMessage中出现异常_C#_.net_Http_Windows 8_Last.fm - Fatal编程技术网

C# .NET4.5中异步POST方法的HttpResponseMessage中出现异常

C# .NET4.5中异步POST方法的HttpResponseMessage中出现异常,c#,.net,http,windows-8,last.fm,C#,.net,Http,Windows 8,Last.fm,我正在用C#为Windows 8(消费者预览)开发一个应用程序。它对Last.fm API进行简单的REST调用 有一个例外,这是困扰我,因为许多天了。我正在尝试向Last.fm API方法发送POST调用。但无论何时我打电话,我都会收到以下mssg- “mscorlib.dll中发生'System.Net.Http.HttpRequestException'类型的异常,但未在用户代码中处理”。 其他信息:发送请求时出错 如果我打印出异常,它会说- System.Net.Http.HttpReq

我正在用C#为Windows 8(消费者预览)开发一个应用程序。它对Last.fm API进行简单的REST调用

有一个例外,这是困扰我,因为许多天了。我正在尝试向Last.fm API方法发送POST调用。但无论何时我打电话,我都会收到以下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)上

我对last.fm的身份验证API的GET调用工作正常

我正在附加一个代码段:

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...

}
请告诉我是否需要堆栈跟踪


-萨加尔

我想我明白了。我保留这个职位供其他人参考

情况是Last.fm服务器不接受标头字段中的Expect:100Continue。所以我必须明确地把它改为false

因此,必须添加以下内容:

HttpClient cli = new HttpClient();
cli.DefaultRequestHeaders.ExpectContinue = false; 

非常感谢。我已经努力解决这个问题好几天了,但我在任何地方都找不到解决方案。你救了我一天。