My Windows 10 UWP app is calling a WebAPI web service that I have created. I need to send a JPG file from the UWP app to the server so that the server can store it into another application.
I am using using Windows.Web.Http;
as recommended for UWP and using Windows Authentication to connect to the server.
When I perform a POST using the following code, I get the IRandomAccessStream does not support the GetInputStreamAt method because it requires cloning error shown below.
I am able to POST HttpStringContent
to the same web service and receive the responses without any issue.
The issue is when trying to send a file to the web service using HttpStreamContent
.
public async void Upload_FileAsync(string WebServiceURL, string FilePathToUpload)
{
//prepare HttpStreamContent
IStorageFile storageFile = await StorageFile.GetFileFromPathAsync(FilePathToUpload);
IBuffer buffer = await FileIO.ReadBufferAsync(storageFile);
byte[] bytes = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(buffer);
Stream stream = new MemoryStream(bytes);
Windows.Web.Http.HttpStreamContent streamContent = new Windows.Web.Http.HttpStreamContent(stream.AsInputStream());
//send request
var myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
myFilter.AllowUI = false;
var client = new Windows.Web.Http.HttpClient(myFilter);
Windows.Web.Http.HttpResponseMessage result = await client.PostAsync(new Uri(WebServiceURL), streamContent);
string stringReadResult = await result.Content.ReadAsStringAsync();
}
Full Error:
{System.NotSupportedException: This IRandomAccessStream does not support the GetInputStreamAt method because it requires cloning and this stream does not support cloning. at System.IO.NetFxToWinRtStreamAdapter.ThrowCloningNotSuported(String methodName) at System.IO.NetFxToWinRtStreamAdapter.GetInputStreamAt(UInt64 position) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at }
Please help!