我一直在编码一个库,其中包含从在线服务到存储处理的大多数基本功能,而且我一直陷在一种方法中,该方法应该将图像上传到主机,该主机将返回文件的路径。
问题是当我尝试拨打电话时,由于堆栈溢出,应用程序崩溃了。
这是完整的方法:
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 );
}
}
最佳答案
StackOverflowException的起源在这里:
httpMultipartContent.Add( httpMultipartContent );
我向该对象添加了对象,这可以自我说明。我交换了变量,它应该是这样的:
httpMultipartContent.Add( httpStreamContent );
关于c# - HttpClient.SendRequestAsync触发StackOverflow,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34634246/