使用 C# 向 SII 提交问题 EnvioDTE

Submittng Issue EnvioDTE to SII using c#

我正在使用 C# 将 Envio DTE 提交到智利的 SII。

现在我收到 html 回复,并收到此

"HA OCURRIDO UN ERROR EN EL UPLOAD DEL ARCHIVO DE DOCUMENTOS TRIBUTARIOS ELECTRONICOS."

我认为参数传递不正确。参数名称在他们的文档中是正确的。此请求应模拟网络浏览器。

如果有人能帮助我,我将不胜感激。 代码:

       public string SendEnvioDte(string fileName, string token, Documento documento, string userRut)
       {
            string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
            
            NameValueCollection nvc = new NameValueCollection();
            var url = "https://maullin.sii.cl/cgi_dte/UPL/DTEUpload";
            var RutEmisor = documento.Encabezado.Emisor.RUTEmisor;
            var RutReceptor = documento.Encabezado.Receptor.RUTRecep;
            var rutSender = userRut.Substring(0, 8);
            var senderDv = userRut.Substring(8);
            var rutCmopany = RutEmisor.Substring(0, 8);
            var rutCompnaydv = RutEmisor.Substring(9);
            nvc.Add("rutSender", rutSender);
            nvc.Add("dvSender", senderDv);
            nvc.Add("rutCompany", rutCmopany);
            nvc.Add("dvCompany", rutCompnaydv);

       
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "multipart/form-data; boundary=" +
                                    boundary;
            request.Method = "POST";
            request.KeepAlive = true;
            request.Headers.Add("POST", "/cgi_dte/UPL/DTEUpload HTTP/1.0");
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36";
            request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/ms-excel,application/msword, */*" ;
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.Headers.Add("Cache-Control", "no-cache");
            request.Headers.Add("Cookie", "TOKEN=" + token );
            request.Headers.Add("Accept-Language", "es-cl" );
            request.Headers.Add("Cache-Control", "no-cache");
            request.Headers.Add("Referer", "http://empresaabc.cl/test.html");
            Stream memStream = new System.IO.MemoryStream();

            var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
                                                                    boundary + "\r\n");
            var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
                                                                        boundary + "--");


            string formdataTemplate = "\r\n--" + boundary +
                                        "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

           
            foreach (string key in nvc.Keys)
            {
                string formitem = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }
           

            string headerTemplate = 
                "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1};\"\r\n" +
                "Content-Type: text/xml \r\n\r\n";

         
            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            var header = string.Format(headerTemplate, "file", "Enivo_DTE_test.xml");
            var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

            memStream.Write(headerbytes, 0, headerbytes.Length);

            using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                var buffer = new byte[1024];
                var bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    memStream.Write(buffer, 0, bytesRead);
                }
            }
          

            memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            request.ContentLength = memStream.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                memStream.Position = 0;
                byte[] tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();
                requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            }

            using (var response = request.GetResponse())
            {
                Stream stream2 = response.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);
            var reuslt = reader2.ReadToEnd();
                return reuslt;
            }
       }

应该是tcp/ip套接字通信加密数据模拟浏览器

改变

System.Text.Encoding.UTF8.GetBytes()
System.Text.Encoding.ASCII.GetBytes()

System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes()

应该可以。