- From: chetanya patharia <chetanya.patharia@gmail.com>
- Date: Thu, 23 Aug 2012 19:51:12 -0700
- To: www-validator@w3.org
- Message-ID: <CANGeoSPyzPtP37NaiosJiS7dpN9HpCwjxLJkGnUWEE1w_PAXxQ@mail.gmail.com>
Hi, I used following code for file upload API, but not able to get the correct error response, pleas help: private static void UploadFilesToRemoteUrl(string url, string[] files, stringlogpath) { 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.Accept = "text/html, application/xhtml+xml"; httpWebRequest2.Referer = "http://validator.w3.org/"; httpWebRequest2.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; httpWebRequest2.Credentials = System.Net. CredentialCache.DefaultCredentials; Stream memStream = new System.IO.MemoryStream(); byte [] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); memStream.Write(boundarybytes,0,boundarybytes.Length); length += boundarybytes.Length; string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\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]); byte [] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes,0,headerbytes.Length); length += 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); length += bytesRead; } memStream.Write(boundarybytes,0,boundarybytes.Length); length += 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); System.IO. File.WriteAllText("test.html", reader2.ReadToEnd()); Console ..WriteLine(reader2.ReadToEnd()); webResponse2.Close(); httpWebRequest2 = null; webResponse2 = null; } static void Main(string[] args) { string urlOfTool = "http://validator.w3.org/check"; string [] fileToValidate ={ @"D:\testfile.htm"}; UploadFilesToRemoteUrl(urlOfTool,fileToValidate, @"D:\"); } -- Thanks & Regards Chetanya Patharia
Received on Sunday, 26 August 2012 12:18:44 UTC