// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Net; public class DataAndFileUpload { private const string BOUNDARY = "ArneArne"; public static void Main(string[] args) { upload("http://localhost:8080/test/xuploaddo.jsp", "test", "C:\\last.cpp"); } public static void upload(string url, string description, string filename) { ServicePointManager.Expect100Continue = false; // to avoid .NET - Tomcat 5 bug HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.KeepAlive = false; req.ContentType = "multipart/form-data; boundary=" + BOUNDARY; StreamWriter post = new StreamWriter(req.GetRequestStream()); post.WriteLine("--" + BOUNDARY); post.WriteLine("Content-Disposition: form-data; name=\"beskrivelse\""); post.WriteLine(""); post.WriteLine(description); post.WriteLine("--" + BOUNDARY); post.WriteLine("Content-Disposition: form-data; name=\"fil\"; filename=\"" + filename + "\""); //post.WriteLine("Content-Type: text/plain"); post.WriteLine("Content-type: application/octet-stream"); post.WriteLine("Content-Transfer-Encoding: binary"); post.WriteLine(""); post.Flush(); Stream binf = new FileStream(filename, FileMode.Open); int c; while((c = binf.ReadByte()) >= 0) { post.BaseStream.WriteByte((byte)c); } binf.Close(); post.BaseStream.Flush(); post.WriteLine("--" + BOUNDARY + "--"); post.Close(); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Console.WriteLine(resp.StatusCode); resp.Close(); } }