Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.34 KB | None | 0 0
  1.  private confirmation SendDocument(string name, string pass, string fileName, byte[] content, string targetName)
  2.         {
  3.  
  4.        
  5.         _progressManager.SetCompleted("Отправка документа " + fileName);
  6.  
  7.             var request =
  8.                 (HttpWebRequest) WebRequest.Create("https://private.bus.gov.ru:443/gmu-integration-web/services/upload.");
  9.             request.ContentType = string.Format("multipart/form-data; boundary={0}", Boundary);
  10.             request.Method = "POST";
  11.             request.KeepAlive = false;
  12.             request.ProtocolVersion = HttpVersion.Version11;
  13.  
  14.             request.Proxy = ConfigurationManager.AppSettings
  15.                 .With(collection => collection.Get("E86n.Proxy"))
  16.                 .With(s => Regex.Match(s, @"http://(\w+):(\w+)@([a-zA-Z_0-9.]+):(\d{4})"))
  17.                 .With(
  18.                     match =>
  19.                         new WebProxy(match.Groups[3].Value, Convert.ToInt32(match.Groups[4].Value))
  20.                         {
  21.                             Credentials = new NetworkCredential(match.Groups[1].Value, match.Groups[2].Value),
  22.                         });
  23.  
  24.             // вроде как отменяем необходимость добавлять сертификат казначейства
  25.             ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
  26.             string confirmationContent = string.Empty;
  27.             string exceptionContent = string.Empty;
  28.             ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
  29.            
  30.             try
  31.             {
  32.                 using (var requestStream = new MemoryStream())
  33.                 {
  34.                     using (var streamWriter = new StreamWriter(requestStream))
  35.                     {
  36.                         streamWriter.WriteLine("--{0}", Boundary);
  37.                         streamWriter.WriteLine("Content-Disposition: form-data; name=\"login\"");
  38.                         streamWriter.WriteLine();
  39.                         streamWriter.WriteLine(name);
  40.                         streamWriter.WriteLine("--{0}", Boundary);
  41.  
  42.                         streamWriter.WriteLine("Content-Disposition: form-data; name=\"password\"");
  43.                         streamWriter.WriteLine();
  44.                         streamWriter.WriteLine(pass);
  45.                         streamWriter.WriteLine("--{0}", Boundary);
  46.  
  47.                         streamWriter.WriteLine(
  48.                             "Content-Disposition: form-data; name=\"document\";filename=\"{0}_{1}_{2}\"",
  49.                             fileName,
  50.                             DateTime.Now.ToString("yyyymmddhhmmss"),
  51.                             "001");
  52.                         streamWriter.WriteLine("Content-Type: text/xml");
  53.                         streamWriter.WriteLine();
  54.                         var r = Encoding.UTF8.GetChars(content);
  55.                         string rs = new string(r);
  56.                         streamWriter.WriteLine(Encoding.UTF8.GetChars(content));
  57.  
  58.                         streamWriter.WriteLine("--{0}--", Boundary);
  59.  
  60.                         streamWriter.Flush();
  61.                         request.ContentLength = streamWriter.BaseStream.Length;
  62.  
  63.                        
  64.                     }
  65.  
  66.                     string result = Encoding.ASCII.GetString(requestStream.ToArray());
  67.  
  68.                     using (var streamWriter = new BinaryWriter(request.GetRequestStream()))
  69.                     {
  70.                         streamWriter.Write(requestStream.ToArray());
  71.                     }
  72.                 }
  73.  
  74.                 using (
  75.                     StreamReader responseReader = request.GetResponse()
  76.                         .GetResponseStream()
  77.                         .With(stream => new StreamReader(stream)))
  78.                 {
  79.                     confirmationContent = responseReader.ReadToEnd();
  80.                 }
  81.             }
  82.             catch (Exception e)
  83.             {
  84.                 exceptionContent = e.ExpandException();
  85.                 throw;
  86.             }
  87.             finally
  88.             {
  89.                 SaveSendState(fileName, content, confirmationContent, targetName, exceptionContent);
  90.             }
  91.  
  92.             var p = request.ToString();
  93.             return confirmation.Parse(confirmationContent);
  94.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement