So this first thing I did here was to convert the MultipartFormDataContent object into a Byte array.
Byte[] byteArray = await form.ReadAsByteArrayAsync();
I then passed this to a function, which makes the HttpWebRequest().
private string HttpPostRequest(string url, MultipartFormDataContent formData, Byte[] form) { HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); myHttpWebRequest.Method = "POST"; myHttpWebRequest.ContentType = formData.Headers.ContentType.ToString(); myHttpWebRequest.ContentLength = formData.Headers.ContentLength.Value; myHttpWebRequest.ProtocolVersion = HttpVersion.Version10; myHttpWebRequest.ServicePoint.ConnectionLimit = 24; myHttpWebRequest.KeepAlive = false; myHttpWebRequest.Timeout = System.Threading.Timeout.Infinite; myHttpWebRequest.AllowWriteStreamBuffering = false; System.Net.ServicePointManager.Expect100Continue = false; Stream requestStream = myHttpWebRequest.GetRequestStream(); int i = 0; attachAttempt: try { requestStream.Write(form, 0, form.Length); } catch (IOException e) { if (i < 5) { i++; goto attachAttempt; } DialogResult attachFail; string attachFailMessage = e.Message.ToString() + " Please check your network and try again."; string attachFailCaption = "Network error."; MessageBoxButtons emailFailButtons = MessageBoxButtons.OK; attachFail = MessageBox.Show(attachFailMessage, attachFailCaption, emailFailButtons, MessageBoxIcon.Error); return ""; } requestStream.Close(); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); Stream responseStream = myHttpWebResponse.GetResponseStream(); StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default); string pageContent = myStreamReader.ReadToEnd(); myStreamReader.Close(); responseStream.Close(); myHttpWebResponse.Close(); return pageContent; }
I compiled this after researching several suggestions related to this area. You may find that you may not need to set some of the flags on the myHttpWebRequest object, but this is what worked for me. I also found that issues on your network will impact this severely as the connection between your host machine and the destination machine may get aborted and it is for this reason I have put the Write() into a try and catch block in case I need to output an error.