static void Main(string[] args)
        {
            //string URLAuth = "https://technet.rapaport.com/HTTP/Authenticate.aspx";
            //WebClient webClient = new WebClient();

           
            //formData["Username"] = "myUser";
            //formData["Password"] = "myPassword";

            //byte[] responseBytes = webClient.UploadValues(URLAuth, "POST", formData);
            //string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
            //webClient.Dispose();

            string URL = "http://localhost:8302/upload";
            string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
            System.Net.WebRequest webRequest = System.Net.WebRequest.Create(URL);

            webRequest.Method = "POST";
            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;

            string FilePath = @"C:\Users\Public\Pictures\Sample Pictures\aa.jpg";
            NameValueCollection formData = new NameValueCollection();
            formData["ReplaceAll"] = "false";

            Stream postDataStream = GetPostStream(FilePath, formData, boundary);

            webRequest.ContentLength = postDataStream.Length;
            Stream reqStream = webRequest.GetRequestStream();

            postDataStream.Position = 0;

            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            while ((bytesRead = postDataStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                reqStream.Write(buffer, 0, bytesRead);
            }

            postDataStream.Close();
            reqStream.Close();

            StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
            string Result = sr.ReadToEnd();
        }
View Code

相关文章: