I have the same problem like this one Here but using the WebClient class on my client and also the second code example from this answere.
So what can I do to only get one call from my WebClient client?
My httplistener callback gets called twice, the first one is allright but the second one throws this error on at HttpListenerContext context = Listener.EndGetContext(ar);
System.Net.HttpListenerException: 'The I/O operation has been aborted because of either a thread exit or an application request'
Server Code:
private void DoWork(object arg)
{
Listener = new HttpListener();
Listener.Prefixes.Add("https://+:28210");
Listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
Console.WriteLine("Listening...");
Listener.Start();
Listener.BeginGetContext(ListenerContext, null);
Console.ReadKey();
}
`
private static void ListenerContext(IAsyncResult ar)
{
Console.WriteLine("Get Data...");
HttpListenerContext context = Listener.EndGetContext(ar);
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)context.User.Identity;
Listener.BeginGetContext(ListenerContext, null);
Console.WriteLine("Got Data!");
//Some more Code...
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseData);
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
}
Client Code:
using (WebClient client = new WebClient())
{
string serialisedData = JsonConvert.SerializeObject(Data);
client.Credentials = new NetworkCredential(config.UserData.Username, config.UserData.Password);
byte[] responsebyte = client.UploadData(config.ServerAddress, System.Text.Encoding.UTF8.GetBytes(serialisedData));
response = System.Text.Encoding.UTF8.GetString(responsebyte);
}