I am developing windows app after developing iOS and android application. I am using shared project with iOS android and windows. That shared project contains service layer for sending get and pose request on server. On iOS and android I am using sync request, while in windows sync doesn't work so I am changing it to async and I am getting following exception.
I am getting following error
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll
Here is my fuction which I am using in service layer to send get and post request.
public async void WindowsApiCall(string url, string method, string contenttype, string body)
{
string response = "";
try
{
var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = method;
request.ContentType = "application/json";
byte[] data = Encoding.UTF8.GetBytes(body);
//request.ContentLength = data.Length;
if (method == "POST")
{
using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, request))
{
await requestStream.WriteAsync(data, 0, data.Length);
}
}
WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
var responseStream = responseObject.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();
response = received;
}
catch (FileNotFoundException aex)
{
string messages = "";
messages += aex.Message + "\r\n";
//Console.WriteLine(messages);
}
}
Please help me out.