0

I am making a cross-platform app using xamarin forms, and I need to make Http post in the background.

I managed to do that with a foreground service and HttpClient in Android.

I am not able to do it in IOS, I am using NSUrlSession, for the backgrounding task.

I was able to do a POST with

application/x-www-form-urlencoded; charset=UTF-8 as a Content-Type.

But I wasn't able to do it with multipart form-data.

I've read somewhere that I have to build the body of the request myself so I did it using some swift and objective C translations, but I was unsuccesful.

I tried to transform this objective c code in this answer to c# and I end up with this code, but it doesn't work, Please help!

    using (var url = NSUrl.FromString(UploadUrlString))
            using (var request = new NSMutableUrlRequest(url))
            {
                string boundaryConstant = "------WebKitFormBoundaryXXXXXXXXXX";
                request.HttpMethod = "POST";
                request["Cookie"] = "SERVERIDXXX=XXXXXX";
                var data = NSData.FromArray(ImageByteArray);
                var uiimage = UIImage.LoadFromData(data);
                NSData img = uiimage.AsJPEG(1);
                string Body = boundaryConstant+ "\r\n";
                Body += "Content-Disposition: form-data; name=\"id\"\r\n\r\n";
                //Body += StaticData.Photos[0].Round;
                Body += 50000+ "\r\n";
                Body += boundaryConstant + "\r\n";
                Body += "Content-Disposition: form-data; name=\"upload_file\"; filename=\"Untitled.png\"\r\n";
                Body += "Content-Type: image/png\r\n\r\n";
                Body+=img+ "\r\n";
                Body += boundaryConstant + "--";
                request.Body = NSData.FromString(Body);
                request["Content-Type"] = "multipart/form-data; boundary="+ boundaryConstant;
                NSUrlSessionDownloadTask downloadTask = session.CreateDownloadTask(request);
                downloadTask.Resume();
            }
Aksen P
  • 4,564
  • 3
  • 14
  • 27
Nicole
  • 134
  • 15
  • Hi , have you solved this , you can show some error log info in question. – Junior Jiang Jul 29 '19 at 07:53
  • No, I didn't the server response is : 500 internal server error. – Nicole Aug 01 '19 at 16:03
  • Okey , could you try postman to request this url content , this will be a good way to check where problem is. – Junior Jiang Aug 02 '19 at 05:51
  • I don't really know how to test this code with postman, I am sending a byte array so ..., but testing with an upload works of course – Nicole Aug 03 '19 at 16:51
  • Maybe problem is the byte array, you add img data with string data , and finally conver it to NSData . This data type can not be used in http , you should check the request of web api ,and modify them be its request. – Junior Jiang Aug 05 '19 at 02:03

1 Answers1

0

Found this link.. Hope it would be helpful Function name - PrepareUpload

https://github.com/dannycabrera/SimpleBackgroundUpload/blob/master/SimpleBackgroundUpload/SimpleBackgroundUpload/AppDelegate.cs

The function is as followed in case the link expires

    /// <summary>
    /// Prepares the upload.
    /// </summary>
    /// <returns>The upload.</returns>
    public async Task PrepareUpload()
    {
        try {
            Console.WriteLine("PrepareUpload called...");

            if (session == null)
                session = InitBackgroundSession();

            // Check if task already exits
            var tsk = await GetPendingTask();
            if (tsk != null) {
                Console.WriteLine ("TaskId {0} found, state: {1}", tsk.TaskIdentifier, tsk.State);

                // If our task is suspended, resume it.
                if (tsk.State == NSUrlSessionTaskState.Suspended) {
                    Console.WriteLine ("Resuming taskId {0}...", tsk.TaskIdentifier);
                    tsk.Resume();
                }

                return; // exit, we already have a task
            }

            // For demo purposes file is attached to project as "Content" and PDF is 8.1MB.
            var fileToUpload = "UIKitUICatalog.pdf";

            if(File.Exists(fileToUpload)) {
                var boundary = "FileBoundary";
                var bodyPath = Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BodyData.tmp");

                // Create request
                NSUrl uploadHandleUrl = NSUrl.FromString (webApiAddress);
                NSMutableUrlRequest request = new NSMutableUrlRequest (uploadHandleUrl);
                request.HttpMethod = "POST";
                request ["Content-Type"] = "multipart/form-data; boundary=" + boundary;
                request ["FileName"] = Path.GetFileName(fileToUpload);

                // Construct the body
                System.Text.StringBuilder sb = new System.Text.StringBuilder("");
                sb.AppendFormat("--{0}\r\n", boundary);
                sb.AppendFormat("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\n", Path.GetFileName(fileToUpload));
                sb.Append("Content-Type: application/octet-stream\r\n\r\n");

                // Delete any previous body data file
                if (File.Exists(bodyPath))
                    File.Delete(bodyPath);

                // Write file to BodyPart
                var fileBytes = File.ReadAllBytes (fileToUpload);
                using (var writeStream = new FileStream (bodyPath, FileMode.Create, FileAccess.Write, FileShare.Read)) {
                    writeStream.Write (Encoding.Default.GetBytes (sb.ToString ()), 0, sb.Length);
                    writeStream.Write (fileBytes, 0, fileBytes.Length);

                    sb.Clear ();
                    sb.AppendFormat ("\r\n--{0}--\r\n", boundary);
                    writeStream.Write (Encoding.Default.GetBytes (sb.ToString ()), 0, sb.Length);
                }
                sb = null;
                fileBytes = null;

                // Creating upload task
                var uploadTask = session.CreateUploadTask(request, NSUrl.FromFilename(bodyPath));
                Console.WriteLine ("New TaskID: {0}", uploadTask.TaskIdentifier);

                // Start task
                uploadTask.Resume (); 
            }
            else
            {
                Console.WriteLine ("Upload file doesn't exist. File: {0}", fileToUpload);
            }   
        } catch (Exception ex) {
            Console.WriteLine ("PrepareUpload Ex: {0}", ex.Message);
        }
    }
ayon.gupta
  • 178
  • 7
  • Sorry but this didn't really help me, I tried to copy his logic but it didn't work, I was trying to make this as simple as possible but this code confused me even more :( . – Nicole Jul 26 '19 at 02:05