473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

upload xml to https with certificate?

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
2 2250
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Hanauer | last post by:
I'm getting my own SSL certificate soon because it is the right thing to do, but until then I have this shopping cart on an ISP that gives me free shared SSL. The cart breaks in HTTPS because of...
4
by: Gary Feldman | last post by:
I think I've found a deficiency in the design of urllib related to https. In order to complete an https connection, it appears that URLOpener and hence FancyURLOpener require the key and cert...
1
by: Ashutosh Bhalerao | last post by:
Hi all, I am trying to write a VB.Net application which communicates over SSL with an IIS server. I have got a temporary certificate from Verisign and installed it on IIS. I am using...
0
by: Jonathan Wax | last post by:
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...
2
by: Matthias Leonhardt | last post by:
Hi, My c#-prog shall call some https URLs at our webserver. It has a self-signed certificate. I also use IE per COM-Control to call a website at our server (also https). How can I install the...
6
by: JIM.H. | last post by:
Hello, I have a web based application for our internet. I see some sites have https, called certificate. I have the following questions: 1. I am wondering if it is really required to have a...
2
by: MarkAurit | last post by:
I have a web service Im using on a machine via http in a dmz that works file. Im now attempting to use it in a second server, this one using https. When I attempt to add the Web Reference in vs.net...
0
by: mahesh anasuri | last post by:
Hi all, I am new to this mailing list. Thankful if any one is using curl/linux version to and worked on Https. I have created certificates (PEM format) for client and server using openSSL. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.