Answered Exception in HttpResponseMessage for async POST method.

  • Wednesday, April 04, 2012 9:25 PM
     
      Has Code

    HI

    This exception is bothering me since many days now. I am trying to send a POST call to a Last.fm API method. But whenever I send a call, I get the following exception -  "An exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll but was not handled in user code".
    Additional information: An error occurred while sending the request. If I print out the exception it says -

    System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.

    My GET calls to the authentication API of last.fm are working fine.

    I am attaching a code snippet:

    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...
    
    }
    Need some help here.

    -Sagar


    • Edited by sagar_sm Wednesday, April 04, 2012 9:26 PM
    •  

All Replies

  • Friday, April 06, 2012 3:16 PM
    Moderator
     
     
    Can you send a reproduction app to me?  MSMALL at Microsoft

    Matt Small - Microsoft Escalation Engineer - Forum Moderator

  • Friday, April 06, 2012 5:09 PM
     
     Answered Has Code

    Hi

    I guess I figured it out. I'm keeping the post for others to refer.

    The case was that Last.fm servers do not accept Expect:100Continue in the header field. So I had to explicitly change it to false. Last.fm hasn't mentioned this issue clearly in their documentation. Took long to figure out...

    Had to add the following:

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


    -Sagar