By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,534 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

upload xml to https with certificate?

P: n/a
Hi,

I spent the last week looking for an answer to the following question:
How can you upload an xml file to an HTTPS server with a specific
certificate.
Basically doing the same as this html form in c# code:

<form method="post" enctype="multipart/form-data"
action="https://www.site.com/upload/"/>
<input type="file" name="xml" id="xml="><br>
<input type="text" name="batchid" id="batchid="><br>
<input type="text" name="schoolcode" id="schoolcode="><br><br>
<input type="submit" value="Submit">
</form>

I wanted to use the WebClient.UploadFile(filename.xml) method but I could
not find how
to do this with a specific Certificate.

Thx
J.

Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jonathan Wax wrote:
Hi,

I spent the last week looking for an answer to the following question:
How can you upload an xml file to an HTTPS server with a specific
certificate.
Basically doing the same as this html form in c# code:

<form method="post" enctype="multipart/form-data"
action="https://www.site.com/upload/"/>
<input type="file" name="xml" id="xml="><br>
<input type="text" name="batchid" id="batchid="><br>
<input type="text" name="schoolcode" id="schoolcode="><br><br>
<input type="submit" value="Submit">
</form>

I wanted to use the WebClient.UploadFile(filename.xml) method but I
could not find how
to do this with a specific Certificate.


You'll hvae to use HttpWebRequest to do that.

Cheers,
--
Joerg Jooss
jo*********@gmx.net
Nov 18 '05 #2

P: n/a
Hi Joerg,

Thank for your feedback. I tried that but the request seems to fail.
I am note sure that my code does exactly what an html form does...

here is my sample code, do you see anything wrong?
--------------------------------
CookieContainer cookies = new CookieContainer();

string uri = uriString;

// cast the WebRequest to a HttpWebRequest since we're using HTTPS

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);

httpWebRequest.Credentials = CredentialCache.DefaultCredentials;

httpWebRequest.CookieContainer = cookies;

//add secure certificate

X509Certificate x509 = X509Certificate.CreateFromCertFile(@"C:\Documents and
Settings\jwax.IMMIGRATIONLAW\My
Documents\SEVIS\mayo\digital_certificates\mayoipo. cer");

httpWebRequest.ClientCertificates.Add(x509);

WebResponse webResponse = httpWebRequest.GetResponse();

string boundary = "----------" + DateTime.Now.Ticks.ToString("x");

HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);

httpWebRequest2.Credentials = CredentialCache.DefaultCredentials;

httpWebRequest2.CookieContainer = cookies;

httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;

httpWebRequest2.Method = "POST";

//add secure certificate

httpWebRequest2.ClientCertificates.Add(x509);

// Build up the post message header

StringBuilder sb = new StringBuilder();

sb.Append("--");

sb.Append(boundary);

sb.Append("\r\n");

sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"");

sb.Append(Path.GetFileName(xmlFile));

sb.Append("\"");

sb.Append("\r\n");

//sb.Append("Content-Type: application/octet-stream");

sb.Append("Content-Type: multipart/form-data");

sb.Append("\r\n");

sb.Append("\r\n");

string postHeader = sb.ToString();

byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);

// Build the trailing boundary string as a byte array

// ensuring the boundary appears on a line by itself

byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary +
"\r\n");

FileStream fileStream = new FileStream(xmlFile, FileMode.Open,
FileAccess.Read);

long length = postHeaderBytes.Length + fileStream.Length +
boundaryBytes.Length;

httpWebRequest2.ContentLength = length;

Stream requestStream = httpWebRequest2.GetRequestStream();

// Write out our post header

requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

// Write out the file contents

byte[] buffer = new Byte[checked((uint)Math.Min(4096,
(int)fileStream.Length))];

int bytesRead = 0;

while ( (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 )

requestStream.Write(buffer, 0, bytesRead);
// Write out the trailing boundary

requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

WebResponse webResponse2 = httpWebRequest2.GetResponse();

//Read Response into String

StreamReader reader = new StreamReader(webResponse2.GetResponseStream() );

string responseString = reader.ReadToEnd();

reader.Close();

Console.Write(responseString);

return responseString;

--------------------------------

Thx,
Jonathan

"Joerg Jooss" <jo*********@gmx.net> wrote in message
news:uk**************@TK2MSFTNGP11.phx.gbl...
Jonathan Wax wrote:
Hi,

I spent the last week looking for an answer to the following question:
How can you upload an xml file to an HTTPS server with a specific
certificate.
Basically doing the same as this html form in c# code:

<form method="post" enctype="multipart/form-data"
action="https://www.site.com/upload/"/>
<input type="file" name="xml" id="xml="><br>
<input type="text" name="batchid" id="batchid="><br>
<input type="text" name="schoolcode" id="schoolcode="><br><br>
<input type="submit" value="Submit">
</form>

I wanted to use the WebClient.UploadFile(filename.xml) method but I
could not find how
to do this with a specific Certificate.


You'll hvae to use HttpWebRequest to do that.

Cheers,
--
Joerg Jooss
jo*********@gmx.net

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.