4

Problem: I have a Java spring rest service to upload a file (large size). I want use a .NET httpClient (or other .net client) to call upload service.

Questions:

  1. It seems that best option to send large file is multi-part file, what's about interoperability ?
  2. If it weren't possible, what is the best alternative ?

Thank you!

3 Answers 3

4

This is the answer: I can send a file with multipart attachment from c# client to 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;
        }
Sign up to request clarification or add additional context in comments.

Comments

1

HTTP is a standard that is independent of OS platforms and programming languages, so you shouldn't have any problems with interoperability in case your .net client complies with the standards.

2 Comments

Ok, this is the idea, from Michael Teper ASP.NET WebApi: how to perform a multipart post with file upload using WebApi HttpClient I will try to simulate a multi-part file upload post using the HttpClient API.
I'm not familiar with that .net client library, but I suppose it generates a standard multipart post request. I guess, the java/spring service on the server side doesn't make any assumptions about the type of the client, just accepts normal multipart requests, so they should just work together.
0

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...

c#

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()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.