Reputation: 491
I'm currently trying to submit a form containing several files to its destination server but do not know how to send this form data off using HttpWebRequest. The form is of type MultipartFormDataContent. I have so far seen ways to submit a form using HttpWebRequest but this is done by constructing the form as a string and then converting it into a byte array. So something like this,
string postData = "";
foreach (string key in postParameters.Keys)
{
postData += HttpUtility.UrlEncode(key) + "="
+ HttpUtility.UrlEncode(postParameters[key]) + "&";
}
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myHttpWebRequest.Method = "POST";
byte[] data = Encoding.ASCII.GetBytes(postData);
Stream requestStream = myHttpWebRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
However, my form is not a string but of type MultipartFormDataContent and so I'm not sure what to do.
Upvotes: 1
Views: 6001
Reputation: 491
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.
Upvotes: 3