Unable to upload file using HTTP POST - c#

I'm trying to upload a file using HTTP Post but somehow there is no file to be found when I process the request on the server side. I was able to create a similar request and successfully upload file using Chrome's Postman extension, but somehow can't do the same programmatically.
Client code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUrl);
request.Method = "POST";
using (Stream requestStream = request.GetRequestStream())
{
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
string header = string.Format(headerTemplate, "Files", "myFile.xml", "text/xml");
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
requestStream.Write(boundarybytes, 0, boundarybytes.Length);
requestStream.Write(headerbytes, 0, headerbytes.Length);
requestStream.Write(uploadedFile, 0, uploadedFile.Length);
requestStream.Write(trailer, 0, trailer.Length);
}
The request looks like this (in Fiddler) :
POST https://host/myUrl
Content-Length: 1067
Expect: 100-continue
Connection: Keep-Alive
------------8d2942f79ab208e
Content-Disposition: form-data; name="Files"; filename="myFile.xml"
Content-Type:text/xml
<myFile>
Something
</myFile>
------------8d2942f79ab208e
Server side:
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count != 1)
return BadRequest("Didn't get the file.");
But I always get httpRequest.Files.Count to be zero. Why?
The following request (created using Postman) gives me httpRequest.Files.Count to be one, as expected.
POST myUrl HTTP/1.1
Host: host
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="Files"; filename="myFile.xml"
Content-Type: text/xml
----WebKitFormBoundaryE19zNvXGzXaLvS5C
What am I doing wrong?

Figured it out. Thanks to this blog
Made two changes:
1) Added ContentType :
request.ContentType = "multipart/form-data; boundary=" + boundary;
2) Modified how the boundary ends
byte[] trailer = System.Text.Encoding.UTF8.GetBytes("\r\n--" + boundary + "--");
And it works now. Hope this helps someone.

Perhaps you need to set the request's content type to "multipart/form-data"
request.ContentType = "multipart/form-data";

Related

using HttpWebRequest upload file (multipart/form-data)

I was trying to upload file to web server through API but i got error
"The remote server returned an error: (403) Forbidden." I also try upload file via postman and it was successful.Below my code uploading file and postman preview.
public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
{
MessageBox.Show(string.Format("Uploading {0} to {1}", file, url));
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
var boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
var wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = CredentialCache.DefaultCredentials;
var rs = wr.GetRequestStream();
const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
var formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.Write(boundarybytes, 0, boundarybytes.Length);
const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
var header = string.Format(headerTemplate, paramName, file, contentType);
var headerbytes = Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
var buffer = new byte[4096];
var bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
var trailer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
var stream2 = wresp.GetResponseStream();
var reader2 = new StreamReader(stream2);
MessageBox.Show(string.Format("Response is: {0}", reader2.ReadToEnd()));
}
catch (Exception ex)
{
MessageBox.Show("Error uploading file" + ex);
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
}
this is postman preview
POST / HTTP/1.1
Host: buckname.s3.amazonaws.com
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="key"
2014/6b9830b098c9871c6356a5e55af91bfd/${filename}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="AWSAccessKeyId"
AKIAJNXK6LUBUSZBXJIQ
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="acl"
public-read
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="policy"
eyJleHBpcmF0aW9uIjoiMjAxNC0wOC0yMlQxMDo1MTow
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="signature"
ezYsOF/P6bGcOqQdyJeE7iApu2A=
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="success_action_status"
201
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="secure"
true
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="x-amz-storage-class"
REDUCED_REDUNDANCY
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file"; filename="exp.png"
Content-Type: image/png
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="Content-Type"
image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
edit (code sending)
var nvc = new NameValueCollection
{
{"AWSAccessKeyId", fields.AWSAccessKeyId},
{"Content-Type", "image/jpeg"},
{"acl", fields.acl},
{"key", fields.key},
{"policy", fields.policy},
{"secure", secure.ToString()},
{"signature", signature},
{"success_action_status", fields.success_action_status},
{"x-amz-storage-class", "REDUCED_REDUNDANCY"}
};
HttpUploadHelper.HttpUploadFile(responsFieldsDeSerial.url.ToString(),#"C:\1.png", "file", "image/jpeg",nvc);
thank you!
My error was
<?xml version="1.0" encoding="UTF-8"?> <Error><Code>AccessDenied</Code><Message>Invalid according to Policy: Policy Condition failed: ["eq", "$Secure", "true"]</Message>
Problem was value of field (in my case : secure) case senstive i tried to post True but it must be true.

C# CF Post Request | Internal server error 500

I want to keep it as simple as possible -
-We switched from an Godaddy windows hosting to Godaddy linux hosting
-After switching i get internal server error 500 when iam using POST from an c#
program(compact framework) with the content type - multipartformdata,
However i can still use a normal html form that sits on the server to post with no issues.
-Everything worked absolutly fine before the switch! I can still do GET requests,
and even normal(Not multipart/form-data) POST request from my C# app .
Headers and content from the request / response using the code below :
Request headers -
POST /users/test.php HTTP/1.1
Content-Type: multipart/form-data; boundary=----------------------------8d0cb969d25c418
Host: mydomain(i censor)
Content-Length: 403
Expect: 100-continue
Connection: Keep-Alive
Request content -
------------------------------8d0cb969d25c418
Content-Disposition: form-data; name="title";
test
------------------------------8d0cb969d25c418
Content-Disposition: form-data; name="pwd";
testpwd
------------------------------8d0cb969d25c418
Content-Disposition: form-data; name="file"; filename=test.txt
Content-Type: text/html
testfile
------------------------------8d0cb969d25c418
Response headers -
HTTP/1.1 500 Internal Server Error
Date: Fri, 20 Dec 2013 10:32:01 GMT
Server: Apache mod_fcgid/2.3.10-dev
Content-Length: 662
Keep-Alive: timeout=5
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
This is my C# Code(Note - I must use compact framework only) :
openFileDialog1.ShowDialog();
NameValueCollection formsdata = new NameValueCollection();
formsdata.Add("title", textBox3.Text);
formsdata.Add("pwd", textBox4.Text);
string[] names = new string[1] { openFileDialog1.FileName };
UploadFilesToRemoteUrl("http://mydomain/users/writefile.php", names, "notused", formsdata, openFileDialog1.SafeFileName);
and for the method UploadFilesToRemoteUrl -
public static void UploadFilesToRemoteUrl(string url, string[] files, string
logpath, NameValueCollection nvc, string filename)
{
long length = 0;
string boundary = "----------------------------" +
DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" +
boundary;
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Credentials =
System.Net.CredentialCache.DefaultCredentials;
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
boundary + "\r\n");
string formdataTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=" + filename + "\r\n Content-Type: text/html\r\n\r\n";
for (int i = 0; i < files.Length; i++)
{
//string header = string.Format(headerTemplate, "file" + i, files[i]);
string header = string.Format(headerTemplate, "file", files[i]);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(files[i], FileMode.Open,
FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
fileStream.Close();
}
httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
WebResponse webResponse2 = httpWebRequest2.GetResponse();
Stream stream2 = webResponse2.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
MessageBox.Show(reader2.ReadToEnd());
webResponse2.Close();
httpWebRequest2 = null;
webResponse2 = null;
}
Any help is really highly appreciated!

how to upload file via c# post request? ownCloud

i`m using ownCloud (open source cloud) and i have a form to upload files
the form sending the post request to upload.php file that handle the upload.
the request have a lot of fields and need to send all info and cookie.
i need to develop a c# code to upload files to the cloud.
the best way in my opinion is to make a request similar to the request that the form does.
what do you think? any suggestions?
p.s i read the following solutions but it is not working.
Sending Files using HTTP POST in c#
http://bytes.com/topic/c-sharp/answers/268661-how-upload-file-via-c-code
Upload files with HTTPWebrequest (multipart/form-data)
thanks
here is some of the code:
the form:
<form data-upload-id='1'
id="data-upload-form"
class="file_upload_form"
action="<?php print_unescaped(OCP\Util::linkTo('files', 'ajax/upload.php')); ?>"
method="post"
enctype="multipart/form-data"
target="file_upload_target_1">
<input type="hidden" name="MAX_FILE_SIZE" id="max_upload" value="<?php p($_['uploadMaxFilesize']) ?>">
<input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>" id="requesttoken">
<input type="hidden" class="max_human_file_size" value="(max <?php p($_['uploadMaxHumanFilesize']); ?>)">
<input type="hidden" name="dir" value="<?php p($_['dir']) ?>" id="dir">
<input type="file" id="file_upload_start" name='files[]'/>
</form>
This the way the request seen to me:
enter code here
Request URL:http://my-url/owncloud/index.php/apps/files/ajax/upload.php
Request Method:POST
Status Code:200 OK
Request Headersview parsed
POST /owncloud/index.php/apps/files/ajax/upload.php HTTP/1.1
Host: my-url
Connection: keep-alive
Content-Length: 730
Accept: */*
requesttoken: 0bbcd458174e76e139ad
Origin: http://my-url
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Referer: http://my-url/owncloud/index.php/apps/files
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: oc_username=tal; oc_token=f24c041e992624d10cabbaa16aa6aeea; oc_remember_login=1; __utma=220528984.2016256779.1375771228.1375771228.1375862096.2; __utmz=220528984.1375771228.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); olfsk=olfsk4827894743066281; hblid=QHgh67nWTyXfpzWe6B1Tj9Z3JM0QBCfA; 510e8a1de6274=eqavm1nikkon6ush7con3o6ar6
Request Payload
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Content-Disposition: form-data; name="MAX_FILE_SIZE"
537919488
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Content-Disposition: form-data; name="requesttoken"
0bbcd458174e76e139ad
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Content-Disposition: form-data; name="dir"
/
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT
Content-Disposition: form-data; name="files[]"; filename="bg.png"
Content-Type: image/png
------WebKitFormBoundaryVhC3ZFEhWXiSUZYT--
Response Headersview parsed
HTTP/1.1 200 OK
Date: Sun, 08 Sep 2013 07:21:02 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze16
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Content-Type-Options: nosniff
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 132
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: text/plain; charset=utf-8
i tried this code
public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream rs = wr.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, paramName, file, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
//StreamReader reader3 = new StreamReader(rs);
rs.Close();
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
MessageBox.Show((string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd())));
Debug.WriteLine(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
}
catch(Exception ex)
{
Debug.WriteLine("Error uploading file", ex);
if(wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
}
the calling function:
NameValueCollection nvc = new NameValueCollection();
nvc.Add("id", "TTR");
nvc.Add("btn-submit-photo", "Upload");
FilesClass.HttpUploadFile("http://192.168.49.108/owncloud/index.php/apps/files/ajax/upload.php", #"C:\t.txt", "files[]", "text/plain", nvc);
the response from the server is {"data":{"message":"Authentication error"},"status":"error"}
that mean that i was rejected by upload.php
maybe i need to send the cookie?
Instead of trying to simulate a POST upload (which ownCloud makes fairly difficult due to security issues) you can use WebDAV to upload the file.
Simply send a PUT request to http://example.com/owncloud/remote.php/webdav/some/path

HttpWebRequest returning 404

Our environment has multiple web servers running behind a load balancer. If I modify my host file to go directly to a web server which will by-pass the load balancer, the HttpWebRequest works fine. If I remove the entry in my host file and let the request go through the load balancer it returns 404. However, if I perform the same request using a simple .html file on the same computer I am running the HttpWebRequest from it all works fine whether there is a entry in my host file or not.
Any ideas why it would work through the browser(Chrome) with the html file, but not an HttpWebRequest?
The request is a POST request which uploads a file and 3 other parameters.
Thanks in advance for any ideas.
EDIT: I tried setting my UserAgent to Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1 which is what Chrome sends when I use the html file.
Here is the function I'm using and currently works when no load balancer is in the mix:
private void PostFileToWebServer(byte[] file)
{
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
var request = (HttpWebRequest)WebRequest.Create(_url);
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.KeepAlive = true;
//Time in milliseconds
request.Timeout = 1200000;
request.Host = "ourdomain.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1";
request.CookieContainer = new CookieContainer(100000);
var memStream = new MemoryStream();
byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
string formDataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach(var key in _parameters.AllKeys)
{
string formData = string.Format(formDataTemplate, key, _parameters[key]);
byte[] formDataBytes = Encoding.UTF8.GetBytes(formData);
memStream.Write(formDataBytes, 0, formDataBytes.Length);
memStream.Write(boundarybytes, 0, boundarybytes.Length);
}
string header = string.Format("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\n Content-Type: application/octet-stream\r\n\r\n", _fileName);
byte[] headerBytes = Encoding.UTF8.GetBytes(header);
memStream.Write(headerBytes, 0, headerBytes.Length);
memStream.Write(file, 0, file.Length);
memStream.Write(boundarybytes, 0, boundarybytes.Length);
request.ContentLength = memStream.Length;
Stream requestStream = request.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
var response = request.GetResponse();
Stream stream2 = response.GetResponseStream();
var reader2 = new StreamReader(stream2);
System.Diagnostics.Debug.WriteLine(reader2.ReadToEnd());
response.Close();
}
EDIT: I commented out request.ContentType = "multipart/form-data; boundary=" + boundary; and it works with the load balancer. Anyone have an explanation for this?
Ok, me and Fiddler had a long conversation on the differences between the successful call and the unsuccessful call. Fiddler says the last boundary of the successful called sent from the browser has an extra "--" at the end of it. When I put that, all is well with the world and I can keep my Content-Type set as it is.

HTTP Post as IE6 using C#

I need to do a HTTP POST using C#. It needs to do a postback the same way as an IE6 page.
From the documentation the postback should look like
POST /.../Upload.asp?b_customerId=[O/M1234] HTTP/1.1
Content-length: 12345
Content-type: multipart/form-data; boundary=vxvxv
Host: www.foo.com
--vxvxv
Content-disposition: form-data; name=”File1”; filename=”noColonsSpacesOrAmpersandsInHere”
Content-type: text/xml
<?xml version=”1.0” encoding=”UTF-8”?>
...
<bat:Batch ...
.......
</bat:Batch>
--vxvxv--
I think im having trouble with the boundary characters. I tried setting the boundary in the post data and fiddler shows something similar but I get a page back with the error "Invalid procedure call or argument". the Content-disposition is in the body rather than the header to keep it within the boundaries. Im not sure that is right. Am I setting the boundary the correct way? Can anyone give some guidance on how to do an IE6 style HTTP POST using C# ? Thanks
My Code
data = "--vxvxv" + Environment.NewLine +
"Content-disposition: form-data; name=\"File1\";" + Environment.NewLine +
"filename=\"provideTest.xml\"" + Environment.NewLine +
"Content-type: text/xml" + Environment.NewLine +
#"<?xml version=""1.0"" encoding=""UTF-8""?>" + Environment.NewLine +
data + Environment.NewLine +
"--vxvxv--";
var encoding = ASCIIEncoding.UTF8;
HttpWebRequest request;
var postData = encoding.GetBytes(data);
request = (HttpWebRequest)WebRequest.Create(url);
request.ContentLength = postData.Length;
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=vxvxv";
request.Host = "www.foo.com";
request.ContentLength = postData.Length;
X509Certificate2Collection certCollect = new X509Certificate2Collection();
X509Certificate2 cert = new X509Certificate2(#"C:\a\cert.pfx", "password");
certCollect.Add(cert);
request.ClientCertificates = certCollect;
using (Stream writeStream = request.GetRequestStream()) {
writeStream.Write(postData, 0, postData.Length); }
WebResponse webResponse = request.GetResponse();
string output = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
LogEntry.Write("Recieved : " + output);
return output;
Fiddler Output (raw)
POST https://../Upload.asp?b_customerId=%5BO/M1234%5D HTTP/1.1
Content-Type: multipart/form-data; boundary=vxvxv
Host: www.foo.com
Content-Length: 5500
Expect: 100-continue
Connection: Keep-Alive
--vxvxv
Content-disposition: form-data; name="File1";
filename="provideTest.xml"
Content-type: text/xml
<?xml version="1.0" encoding="UTF-8"?>
...SNIP...
</bat:Batch>
--vxvxv--
I think that you have two potential problems:
1) The URL that you are sending formats the b_CustomerId parameter differently than the IE6 implementation. If the site you are targeting does not expect an HTML-encoded value, this could very easily be the source of the error message.
Your request:
Upload.asp?b_customerId=%5BO/M1234%5D
The IE6 request:
Upload.asp?b_customerId=[O/M1234]
In order to fix this issue, you can create a new Url from an overload of the Uri class constructor that has been marked as obsolete, but still works correctly. This overload allows you to specify that the string has already been escaped in the second parameter.
In order to use this constructor, change this line:
request = (HttpWebRequest)WebRequest.Create(url);
to this:
request = (HttpWebRequest)WebRequest.Create(new Uri(url, true));
2) The Content-disposition tag is not formatted the same way in your request as it is in the IE6 request.
Your request:
Content-disposition: form-data; name="File1";
filename="provideTest.xml"
IE6 request:
Content-disposition: form-data; name=”File1”; filename=”noColonsSpacesOrAmpersandsInHere”
This can be resolved by changing these two lines:
"Content-disposition: form-data; name=\"File1\";" + Environment.NewLine +
"filename=\"provideTest.xml\"" + Environment.NewLine +
to:
"Content-disposition: form-data; name=\"File1\"; " +
"filename=\"provideTest.xml\"" + Environment.NewLine +
I have blogged about a way of uploading multiple files using a WebClient and the possibility to send parameters as well. Here's the relevant code:
public class UploadFile
{
public UploadFile()
{
ContentType = "application/octet-stream";
}
public string Name { get; set; }
public string Filename { get; set; }
public string ContentType { get; set; }
public Stream Stream { get; set; }
}
and then a method to perform the upload:
public byte[] UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
{
var request = WebRequest.Create(address);
request.Method = "POST";
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
request.ContentType = "multipart/form-data; boundary=" + boundary;
boundary = "--" + boundary;
using (var requestStream = request.GetRequestStream())
{
// Write the values
foreach (string name in values.Keys)
{
var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
}
// Write the files
foreach (var file in files)
{
var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
requestStream.Write(buffer, 0, buffer.Length);
file.Stream.CopyTo(requestStream);
buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
}
var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
}
using (var response = request.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var stream = new MemoryStream())
{
responseStream.CopyTo(stream);
return stream.ToArray();
}
}
which could be used like so:
using (var stream1 = File.Open("test.txt", FileMode.Open))
using (var stream2 = File.Open("test.xml", FileMode.Open))
using (var stream3 = File.Open("test.pdf", FileMode.Open))
{
var files = new[]
{
new UploadFile
{
Name = "file",
Filename = "test.txt",
ContentType = "text/plain",
Stream = stream1
},
new UploadFile
{
Name = "file",
Filename = "test.xml",
ContentType = "text/xml",
Stream = stream2
},
new UploadFile
{
Name = "file",
Filename = "test.pdf",
ContentType = "application/pdf",
Stream = stream3
}
};
var values = new NameValueCollection
{
{ "key1", "value1" },
{ "key2", "value2" },
{ "key3", "value3" },
};
byte[] result = UploadFiles("http://localhost:1234/upload", files, values);
}
This won't be a complete answer, but you could look at using a socket instead of WebRequest and performing the HTTP request yourself. It seems that the multipart handler on your server is non-conformi g and expects the request to be the same as IE6 would make, so emulating that yourself would be the best approach.

Categories

Resources