Exemple #1
0
        public string Import(StringBuilder csvContent, ImportSettings settings)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SlemmaURL + "/api/ImportService/import?" + settings.getUrlParams());
            request.Headers.Add("sig", CalculateSHA1(AppSecret + Token, Encoding.UTF8));
            request.Headers.Add("appid", AppKey.ToString());
            request.Method = "POST";
            string boundary = "----WebKitFormBoundaryehqugrHf3XnbBJib";
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            StringBuilder text = new StringBuilder();
            if (settings.Fields != null && settings.Fields.Count > 0)
            {
                JavaScriptSerializer s = new JavaScriptSerializer();

                text.Append("------WebKitFormBoundaryehqugrHf3XnbBJib\r\n");
                text.Append("Content-Disposition: form-data; name=\"fields\"\r\n\r\n");

                text.Append(s.Serialize(settings.Fields) + "\r\n");
            }

            text.Append("--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"fileInput_0\"; filename=\"file.csv\"\r\n" +
                "Content-Type: application/vnd.ms-excel\r\n\r\n");

            text.Append(csvContent);
            text.Append("\r\n--" + boundary + "--");

            byte[] data = Encoding.UTF8.GetBytes(text.ToString());
            request.ContentLength = data.Length;
            Stream rqStream = request.GetRequestStream();
            rqStream.Write(data, 0, data.Length);

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader responseReader = new StreamReader(response.GetResponseStream());
                string fullResponse = responseReader.ReadToEnd();
                response.Close();
                return fullResponse;
            }
            catch (System.Net.WebException e)
            {
                using (StreamReader responseReader = new StreamReader(e.Response.GetResponseStream()))
                {
                    throw new Exception(responseReader.ReadToEnd());
                }
            }
        }
Exemple #2
0
 public string Import(string fullFileName, ImportSettings settings)
 {
     return Import(new StringBuilder(File.ReadAllText(fullFileName)), settings);
 }