C # Uploading an HttpClient file to spring support

Problem: I have a Java spring support service to download a file (large size). I want to use .NET httpClient (or another .net client) to call the download service.

Questions:

  • It seems like the best option for sending a large file is a multi-component file, but what about compatibility?
  • If this were not possible, what is the best alternative?

Thanks!

+4
source share
3 answers

This is the answer: I can send a file with multi-page attachment from a C # client in Java JAX Rest Webservice.

try { using ( var client = new HttpClient()) using (var form = new MultipartFormDataContent()) { using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var fileContent = new StreamContent(stream)) { fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {FileName = fileName, DispositionType = DispositionTypeNames.Attachment, Name = "fileData"}; form.Add(fileContent); // only for test purposes, for stable environment, use ApiRequest class. response = client.PostAsync(url, form).Result; } } } return response.RequestMessage != null ? response.ReasonPhrase : null; } catch (Exception ex) { TraceManager.TraceError("Post Asyn Request to " + url + " \n" + ex.Message, ex); throw; } 
+4
source

HTTP is a standard that does not depend on OS platforms and programming languages, so you should not have compatibility problems if your .net client complies with standards.

+1
source

java spring boot

 @RequestMapping(value="/upload", method=RequestMethod.POST) public @ResponseBody String upload(@RequestParam("FileParam") MultipartFile file){ InputStream fromClient=file.getInputStream(); ...do stuff with the database/ process the input file... 

WITH#

 HttpClient client = new HttpClient(); MultipartFormDataContent form = new MultipartFormDataContent(); FileInfo file = new FileInfo(@"<file path>"); form.Add(new StreamContent(file.OpenRead()),"FileParam",file.Name); HttpResponseMessage response = await client.PostAsync("http://<host>:<port>/upload", form); Console.WriteLine(response.StatusCode); Console.WriteLine(response.ReasonPhrase); Console.WriteLine(response.ToString()); Console.WriteLine(Encoding.ASCII.GetString(await response.Content.ReadAsByteArrayAsync())); 
0
source

All Articles