Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient.SendRequestAsync triggering StackOverflow

I've been coding a library that contains the most of the basic funtions from data handling from online services to storage handling and I've been stuck in a method that should upload an image to a host that would return the path to the file.

The problem is that when I try to make the call, the app crahses due to stack overflow.

Here is the complete method:

public static async Task<WebResult> UploadImage( WebHost webHost, Object webPath, String webQuery, IRandomAccessStream imageStream ) {
    if (!AllowNetworkConnectivity) {
        return new WebResult( false, true, null );
    }

    HttpClient
        httpClient = new HttpClient();

    HttpMultipartFormDataContent
        httpMultipartContent = new HttpMultipartFormDataContent();

    HttpStreamContent
        httpStreamContent = new HttpStreamContent( imageStream );

    httpStreamContent.Headers.Add( "Content-Type", "application/octet-stream" );
    httpMultipartContent.Add( httpMultipartContent );

    try {
        HttpRequestMessage
            httpRequest = new HttpRequestMessage( HttpMethod.Post, new Uri( WebServerUrl( /* ! */ ) ) );

        Dictionary<String, String>
            webCredentialsDictionary = WebServerAuthenticationCredentials( webHost ) as Dictionary<String, String>;

        foreach (KeyValuePair<String, String> credentialEntry in webCredentialsDictionary) {
            httpRequest.Headers.Add( credentialEntry );
        }

        httpRequest.Content = httpMultipartContent;

        /* THE STACK OVERFLOW EXCEPTION IS TRIGGERED HERE */
        HttpResponseMessage
            httpResponse = await httpClient.SendRequestAsync( httpRequest );

        httpResponse.EnsureSuccessStatusCode();

        return new WebResult( true, true, null );
    } catch (Exception exception) {
        AppController.ReportException( exception, "Unable to upload image." );

        return new WebResult( false, true, null );
    }
}
like image 896
auhmaan Avatar asked Jul 18 '26 23:07

auhmaan


1 Answers

The origin of the StackOverflowException was here:

httpMultipartContent.Add( httpMultipartContent );

I was adding the object to itself, which can be self explanatory. I've swapped the variables, this is what it should look like:

httpMultipartContent.Add( httpStreamContent );
like image 112
auhmaan Avatar answered Jul 21 '26 11:07

auhmaan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!