0

I want to send an object (DataSet) From the windows application to the .netcore API

something like this (Windows Application side)

public string submitRequest(Dataset request)
        {
            .
            .
            .
            using (client)
            {

                client.Headers["Content-Type"] = "application/json; charset=utf-8";
                byte[] result = client.UploadValues(Url, "POST", request);
                return Encoding.UTF8.GetString(result);


            }
        }

(.netcore API)

public string submitRequest( values)
        {
            // processing values
        }

I try to convert to json and try NameValueCollection but no result.

  • Don't really now how exactly your whole Client/Server communication looks like but you have to convert the object to binary (by using the `System.Runtime.Serialization.Formatters.Binary.BinaryFormatter` object for example) and then send it in chunks if it is large or send it in one go if it's small. I'd recommend creating a lightweight TCP Client/Server application and send your raw data via TCP. You can also design your own datagram and make sure that the data being received is valid. –  Nov 02 '22 at 13:19
  • 1
    The post request uses a concept known as Content, which would contain the result byte array. I don't understand your code as it's different that a straight HttpPost using the HttpClient in C#. There's plenty of information on this on Microsoft site. Just remember you can and should send the request in async mode. – JWP Nov 02 '22 at 13:45
  • 1
    @Max , there is a "Warning - BinaryFormatter is insecure and can't be made secure. For more information, see the BinaryFormatter security guide." See https://aka.ms/binaryformatter for more information. See also https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter?view=net-6.0 – Stefan Wuebbe Nov 02 '22 at 14:18

1 Answers1

0

It was simple

- On the client side

Create new object :

Class1 SentClass1 = new Class1 ();

Then serialize the object :

string json = JsonConvert.SerializeObject(SentClass1); 

Then send the JSON (UploadString) using WebClient

using (WebClient client = new WebClient()){
       client.Headers[HttpRequestHeader.ContentType]="application/json";
       response = client.UploadString("ourUrl/SubmitRequest", json);
}

On the other hand - On the Server Side

[HttpPost]
[Route("SubmitRequest")]

public void SubmitRequest(Class2 recievedClass){
      return recievedClass.processing()
}

This automatically maps data from (JSON converted from Class1) to Class2 if they have the names of the same attributes if not I can use an auto mapper.

  • 1
    `WebClient` is not recommended for new development ([as per Microsoft](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient?view=net-6.0)), instead you should use [HttpClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-6.0). – risto Nov 06 '22 at 09:20
  • As @risto comment we can use HttpClient instead of WebClient like this https://stackoverflow.com/a/39414248/12450651 – Hosam Eldeen Reda Nov 06 '22 at 09:34