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!

Avi
  • 21,182
  • 26
  • 82
  • 121
Donald
  • 534
  • 1
  • 10
  • 18

3 Answers3

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;
        }
Donald
  • 534
  • 1
  • 10
  • 18
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.

zagyi
  • 17,223
  • 4
  • 51
  • 48
  • Ok, this is the idea, from Michael Teper [ASP.NET WebApi: how to perform a multipart post with file upload using WebApi HttpClient](http://stackoverflow.com/a/10744043) I will try to simulate a multi-part file upload post using the HttpClient API. – Donald Apr 12 '13 at 07:33
  • 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. – zagyi Apr 12 '13 at 07:50
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()));
Austin_Anderson
  • 900
  • 6
  • 16