CSharp

Image Upload

static void Main(string[] args)
{
            string url = "http://localhost:8800/storage/upload";
            string detectUrl = "http://localhost:8800/face/detection/detect";
            string filePath = @"C:\Users\Pictures\lena.jpg";

            string testStr = UploadFileByHttpWebRequest(url, filePath, "image", "image/jpeg");
            JObject o = JObject.Parse(testStr);
            JToken tk = o.GetValue("img_id");
            string detectionStr = DectectByHttpWebRequest(detectUrl, "img_id", tk.ToString());
            System.Console.WriteLine(detectionStr);
}

public static string UploadFileByHttpWebRequest(string url, string file, string paramName, string contentType)
{
             string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
             byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

             HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
             wr.ContentType = "multipart/form-data; boundary=" + boundary;
             wr.Method = "POST";
             wr.KeepAlive = true;
             wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

             Stream rs = wr.GetRequestStream();

             rs.Write(boundarybytes, 0, boundarybytes.Length);

             string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
             string header = string.Format(headerTemplate, paramName, file, contentType);
             byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
             rs.Write(headerbytes, 0, headerbytes.Length);

             FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
             byte[] buffer = new byte[4096];
             int bytesRead = 0;
             while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
             {
                 rs.Write(buffer, 0, bytesRead);
             }
             fileStream.Close();

             byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
             rs.Write(trailer, 0, trailer.Length);
             rs.Close();

             WebResponse wresp = null;
             var result = "";

             System.Console.WriteLine("outside");
             try
             {
                 wresp = wr.GetResponse();
                 Stream stream2 = wresp.GetResponseStream();
                 StreamReader reader2 = new StreamReader(stream2);
                 result = reader2.ReadToEnd();
             }
             catch (Exception ex)
             {
                 System.Console.WriteLine(ex);
                 if (wresp != null)
                 {
                     wresp.Close();
                 }
             }

             wr = null;
             return result;
}

public static string DectectByHttpWebRequest(string url, string paramName, string paramValue)
 {
             string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
             byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

             HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
             wr.ContentType = "multipart/form-data; boundary=" + boundary;
             wr.Method = "POST";
             wr.KeepAlive = true;
             wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

             Stream rs = wr.GetRequestStream();

             string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
             rs.Write(boundarybytes, 0, boundarybytes.Length);
             string formItem = string.Format(formdataTemplate, paramName, paramValue);
             byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(formItem);
             rs.Write(formItemBytes, 0, formItemBytes.Length);

             byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
             rs.Write(trailer, 0, trailer.Length);
             rs.Close();

             WebResponse wresp = null;
             var result = "";
             System.Console.WriteLine("outside");
             try
             {
                 wresp = wr.GetResponse();
                 Stream stream2 = wresp.GetResponseStream();
                 StreamReader reader2 = new StreamReader(stream2);
                 result = reader2.ReadToEnd();
             }
             catch (Exception ex)
             {
                 System.Console.WriteLine(ex);
                 if (wresp != null)
                 {
                     wresp.Close();
                 }
             }

             wr = null;
             return result;
}

results matching ""

    No results matching ""