Re: upload xml to https with certificate?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jonathan Wax (jwax_at_jonathanwax.com)
Date: 06/01/04


Date: Mon, 31 May 2004 19:19:44 -0700

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" <joerg.jooss@gmx.net> wrote in message
news:uk9a4rVREHA.1448@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
> joerg.jooss@gmx.net



Relevant Pages

  • Re: Using HTTPWebRequest to make a web spider but some pages return nothing (groups.google.com for i
    ... Here is some code I have written which reads IE's cookies for a specific URI and returns them in a 'CookieCollection' which can be put in a 'CookieContainer' and placed into 'HttpWebRequest'. ... Dim cookieFolder As String = System.Environment.GetFolderPath ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Automating a POST request
    ... The way you are creating your payload string is nuts. ... Look at the cookies. ... Most sites handle login by sending a cookie on the login form. ... The cookie needs to come back on the Post request. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: FormsAuthentication and Redirection fails
    ... I've figured the reason why this behaviour is happening. ... and it seems that it blocks cookies from http://localhost ... > public static string Authenticate(string EmailAddress, ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: FileUpload
    ... Mich interessier eigentlich nur ob man das was im Beispielcode mit der ... HttpWebRequest Klasse gemacht wird auch mit WebClient.UploadFile möglich ist. ... private bool UploadFileWithPost(string filename, string issueID) ... WebClient webClient = new WebClient; ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: FileUpload
    ... Mich interessier eigentlich nur ob man das was im Beispielcode mit der ... HttpWebRequest Klasse gemacht wird auch mit WebClient.UploadFile möglich ... private bool UploadFileWithPost(string filename, string issueID) ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)