HttpWebRequest on Android

I am working on an app which can upload tracks to SoundCloud. I have been working on this for a while but i can’t get it to work. I am using HttpWebRequest for the request to SoundCloud and this works fine on Unity for Windows. But when i try it on my Android device i get the following message: ‘Request entity contains invalid byte sequence. Please transmit valid UTF-8.’.

Below is the part of code that i use (got it from somewhere on the internet).

I made sure i was uploading the same file on Windows as on Android and did the request to RequestBin. There you can see the requests (for 48 hours), the most bottom one in from Windows, the one above that is from Android. Now, when i compared the two, i noticed that the raw data is almost completely identical except for the end:

47005-requests.png

Notice that on Android there is more data. Can someone explain to me what is going on here?

public static HttpWebResponse Upload(HttpWebRequest req, UploadFile[] files, NameValueCollection form)
	{
		List<MimePart> mimeParts = new List<MimePart>();
		
		try
		{
			foreach (string key in form.AllKeys)
			{
				StringMimePart part = new StringMimePart();
				
				part.Headers["Content-Disposition"] = "form-data; name=\"" + key + "\"";
				part.StringData = form[key];

				mimeParts.Add(part);
			}
			
			int nameIndex = 0;
			
			foreach (UploadFile file in files)
			{
				StreamMimePart part = new StreamMimePart();
				
				if (string.IsNullOrEmpty(file.FieldName))
					file.FieldName = "file" + nameIndex++;
				
				part.Headers["Content-Disposition"] = "form-data; name=\"" + file.FieldName + "\"; filename=\"" + file.FileName + "\"";
				part.Headers["Content-Type"] = file.ContentType;

				part.SetStream(file.Data);
				
				mimeParts.Add(part);
			}
			
			string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
			
			req.ContentType = "multipart/form-data; boundary=" + boundary;
			req.Method = "POST";
			
			long contentLength = 0;
			
			byte[] _footer = Encoding.UTF8.GetBytes("--" + boundary + "--

");

			foreach (MimePart part in mimeParts)
			{
				contentLength += part.GenerateHeaderFooterData(boundary);
			}
			
			req.ContentLength = contentLength + _footer.Length;
			
			byte[] buffer = new byte[8192];
			byte[] afterFile = Encoding.UTF8.GetBytes("

");
int read;

			foreach(var header in req.Headers)
			{
				Debug.Log(header);
			}

			using (Stream s = req.GetRequestStream())
			{
				foreach (MimePart part in mimeParts)
				{
					s.Write(part.Header, 0, part.Header.Length);

					while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
					{
						s.Write(buffer, 0, read);
					}
					
					part.Data.Dispose();
					
					s.Write(afterFile, 0, afterFile.Length);
				}
				
				s.Write(_footer, 0, _footer.Length);
			}

			return (HttpWebResponse)req.GetResponse();
		}
		catch (Exception e)
		{
			Debug.Log ("Crash! Message: " + e.Message);
			foreach (MimePart part in mimeParts)
				if (part.Data != null)
					part.Data.Dispose();
			
			throw;
		}
	}

It’s working!! The problem was that at some point i used StringBuilder.AppendLine() to add a new line. This works fine on Windows, but on Android it didn’t work… (i figured it out because the Content-Length was not the same for Windows and Android.)

I fixed it by instead of using ‘StringBuilding.AppendLine()’, i use ‘StringBuilder.Append("
")’