Ejemplo n.º 1
0
        private static void doMultiPartPOSTs(byte[] xmlData, ConfigPOST config)
        {
            int postCount = 0;

            while (postCount < config.PostTimes)
            {
                printLine(DateTime.Now.ToString());
                Stopwatch stopWatch = new Stopwatch();
                postCount++;
                try {
                    HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create(config.StringURL);
                    //For HTTPS two way cert, must be imported to "Trusted Root Certificate Authorities", Location: IE > Tools > Internet Options > Content > Certificates
                    POSTRequest.ClientCertificates.Add(new X509Certificate(config.SslCertFileName, config.getCertPassword())); // ours/CACI
                    //End HTTPS
                    POSTRequest.Method    = "POST";
                    POSTRequest.KeepAlive = false;
                    POSTRequest.Timeout   = config.ClientTimeout;
                    //Content length of message body
                    POSTRequest.Accept = "application/xml";

                    var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
                    POSTRequest.ContentType = "multipart/form-data; boundary=" + boundary;
                    boundary = "--" + boundary;

                    stopWatch.Start();
                    using (var requestStream = POSTRequest.GetRequestStream()) {
                        var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                        requestStream.Write(buffer, 0, buffer.Length);
                        buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", "file",
                                                                      config.getAttachmentFileName(), Environment.NewLine));
                        requestStream.Write(buffer, 0, buffer.Length);
                        buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", "text/xml", Environment.NewLine));
                        requestStream.Write(buffer, 0, buffer.Length);

                        requestStream.Write(xmlData, 0, xmlData.Length);

                        buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
                        requestStream.Write(buffer, 0, buffer.Length);

                        var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
                        requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
                    }

                    HttpWebResponse POSTResponse = (HttpWebResponse)POSTRequest.GetResponse();
                    StreamReader    reader       = new StreamReader(POSTResponse.GetResponseStream(), Encoding.UTF8);
                    string          daLocation   = POSTResponse.GetResponseHeader("Location");
                    HttpStatusCode  daStatusCode = POSTResponse.StatusCode;

                    string ResponseFromPost = reader.ReadToEnd().ToString();
                    POSTResponse.Close();
                    printLine("StatusCode: " + daStatusCode);

                    if (daStatusCode == HttpStatusCode.Accepted ||
                        daStatusCode == HttpStatusCode.Created ||
                        daStatusCode == HttpStatusCode.OK)
                    {
                        printLine(ResponseFromPost);
                    }
                } catch (WebException we) {
                    String errMsg = we.Message;
                    if (we.Response != null)
                    {
                        errMsg += "; " + new StreamReader(we.Response.GetResponseStream(), Encoding.UTF8).ReadToEnd().ToString();
                    }
                    printLine(errMsg);
                }
                stopWatch.Stop();
                TimeSpan ts          = stopWatch.Elapsed;
                string   elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                     ts.Hours, ts.Minutes, ts.Seconds,
                                                     ts.Milliseconds / 10);
                printLine("Elapsed Time: " + elapsedTime);
                printLine("POST Count: " + postCount + Environment.NewLine);
            }
        }
Ejemplo n.º 2
0
        private static void doMultiPartPOSTs(byte[] xmlData, ConfigPOST config)
        {
            int postCount = 0;
            while (postCount < config.PostTimes) {
                printLine(DateTime.Now.ToString());
                Stopwatch stopWatch = new Stopwatch();
                postCount++;
                try {
                    HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create(config.StringURL);
                    //For HTTPS two way cert, must be imported to "Trusted Root Certificate Authorities", Location: IE > Tools > Internet Options > Content > Certificates
                    POSTRequest.ClientCertificates.Add(new X509Certificate(config.SslCertFileName, config.getCertPassword())); // ours/CACI
                    //End HTTPS
                    POSTRequest.Method = "POST";
                    POSTRequest.KeepAlive = false;
                    POSTRequest.Timeout = config.ClientTimeout;
                    //Content length of message body
                    POSTRequest.Accept = "application/xml";

                    var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
                    POSTRequest.ContentType = "multipart/form-data; boundary=" + boundary;
                    boundary = "--" + boundary;

                    stopWatch.Start();
                    using (var requestStream = POSTRequest.GetRequestStream()) {
                        var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                        requestStream.Write(buffer, 0, buffer.Length);
                        buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", "file",
                                                            config.getAttachmentFileName(), Environment.NewLine));
                        requestStream.Write(buffer, 0, buffer.Length);
                        buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", "text/xml", Environment.NewLine));
                        requestStream.Write(buffer, 0, buffer.Length);

                        requestStream.Write(xmlData, 0, xmlData.Length);

                        buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
                        requestStream.Write(buffer, 0, buffer.Length);

                        var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
                        requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
                    }

                    HttpWebResponse POSTResponse = (HttpWebResponse)POSTRequest.GetResponse();
                    StreamReader reader = new StreamReader(POSTResponse.GetResponseStream(), Encoding.UTF8);
                    string daLocation = POSTResponse.GetResponseHeader("Location");
                    HttpStatusCode daStatusCode = POSTResponse.StatusCode;

                    string ResponseFromPost = reader.ReadToEnd().ToString();
                    POSTResponse.Close();
                    printLine("StatusCode: " + daStatusCode);

                    if (daStatusCode == HttpStatusCode.Accepted ||
                        daStatusCode == HttpStatusCode.Created ||
                        daStatusCode == HttpStatusCode.OK) {
                        printLine(ResponseFromPost);
                    }
                } catch (WebException we) {
                    String errMsg = we.Message;
                    if (we.Response != null)
                        errMsg += "; " + new StreamReader(we.Response.GetResponseStream(), Encoding.UTF8).ReadToEnd().ToString();
                    printLine(errMsg);
                }
                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;
                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                        ts.Hours, ts.Minutes, ts.Seconds,
                                                        ts.Milliseconds / 10);
                printLine("Elapsed Time: " + elapsedTime);
                printLine("POST Count: " + postCount + Environment.NewLine);
            }
        }