M
M
Maxim Skvortsov2016-02-02 03:47:01
C++ / C#
Maxim Skvortsov, 2016-02-02 03:47:01

How to send a POST request with multiparameters in C#?

Hello colleagues!
I'm trying to generate a POST request to the specified address: https://int223.zakupki.gov.ru/223/integration/integration/upload
But every time I try to get a response, the server returns 404.
The regulation describes an example:


An example of a POST request (variable values ​​are indicated in brackets):
POST /223/integration/integration/upload HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------7db10b11c0824
Host: int223.zakupki.gov.ru
Content-Length: (content size)
Connection: Keep -Alive
Cache-Control: no-cache
-----------------------7db10b11c0824
Content-Disposition: form-data; name="login"
(username)
-----------------------7db10b11c0824
Content-Disposition: form-data; name="password"
(password)
-----------------------7db10b11c0824
Content-Disposition: form-data; name="document"; filename="(filename)"
Content-Type: text/xml
(XML Document)
-----------------------7db10b11c0824

Here is the source:
X509Store certStore = new X509Store(StoreLocation.CurrentUser);
            certStore.Open(OpenFlags.ReadOnly);
            var cert = certStore.Certificates.Find(X509FindType.FindBySubjectName, "MyCompanySSL", false)[0];
            
            byte[] fileData = File.ReadAllBytes(@"D:\test.xml");

            const string url = "https://int223.zakupki.gov.ru/223/integration/integration/upload";
            string boundary = $"-----------------------------{DateTime.Now.Ticks.ToString("x")}";
            byte[] data;

            using (Stream formDataStream = new MemoryStream())
            {
                string login = $"{boundary}\r\nContent-Disposition: form-data; name=\"login\"\r\n\r\n{"MyLogin"}";
                formDataStream.Write(Encoding.ASCII.GetBytes(login), 0, Encoding.ASCII.GetBytes(login).Length);
                
                string password = $"{boundary}\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\n{"MyPassword"}";
                formDataStream.Write(Encoding.ASCII.GetBytes(password), 0, Encoding.ASCII.GetBytes(password).Length);
                
                string fileHeader = $"{boundary}\r\nContent-Disposition: form-data; name=\"{"document"}\"; filename=\"{"test.xml"}\"\r\nContent-Type: text/xml\r\n\r\n";
                formDataStream.Write(Encoding.ASCII.GetBytes(fileHeader), 0, Encoding.ASCII.GetBytes(fileHeader).Length);
                formDataStream.Write(fileData, 0, fileData.Length);
                string fileFooter = "\r\n" + boundary + "\r\n";
                formDataStream.Write(Encoding.ASCII.GetBytes(fileFooter), 0, Encoding.ASCII.GetByteCount(fileFooter));
                
                formDataStream.Position = 0;
                data = new byte[formDataStream.Length];
                formDataStream.Read(data, 0, data.Length);
            }
            
            WebRequest request = WebRequest.Create(url);

            ((HttpWebRequest)request).ClientCertificates.Add(cert);
            ((HttpWebRequest)request).CachePolicy= new System.Net.Cache.HttpRequestCachePolicy(System.Net.Cache.HttpRequestCacheLevel.NoCacheNoStore);

            request.Method = "POST";
            request.ContentType = $"multipart/form-data; boundary={boundary}";
            request.ContentLength = data.Length;
            
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }
            
            var response = request.GetResponse() as HttpWebResponse;

GetResponse() keeps getting 404. I can't figure out what's wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Skvortsov, 2016-02-03
@FavoritMVS

Спасибо всем за участие. Проблема решена. Дело было в количестве "-", в заголовке запроса boundary должно содержаться на 2 "--" меньше чем в параметрах запроса...

C
chupasaurus, 2016-02-02
@chupasaurus

Как отправить POST запрос (HTTPS с использованием TLS) (на сайт гос закупок)?

Артур Нуруллин, 2016-02-04
@Splo1ter

Какой то изврат у вас =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question