Exemplo n.º 1
0
        //public void getMarginFromPortfolio(Object obj)
        //{
        //    RunReceiveMargin(int instrumentCnt)
        //}

        //public async void RunReceiveMargin(int instrumentCnt)
        //{

        //}

        async Task RunAsync2()
        //
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://cmecorenr.cmegroup.com/MarginServiceApi/");
                client.DefaultRequestHeaders.Accept.Clear();
                //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("username", "spickering");
                client.DefaultRequestHeaders.Add("password", "NJoyce111174$");
                //client.DefaultRequestHeaders.Add("sessionId", "42D52622EAF1ED384988E02088281401.instance1");

                /*
                 * Headers = {sessionId: 42D52622EAF1ED384988E02088281401.instance1
                 * Pragma: no-cache
                 * Cache-Control: no-store, no-cache
                 * Date: Tue, 27 May 2014 18:10:18 GMT
                 * Server: Apache-Coyote/1.1
                 * Set-Cookie: JSESSIONID=42D52622EAF1ED384988E02088281401.instance1; Path=/coreapi/; H...
                 */

                string[] csvHeaders = new string[13];
                csvHeaders[0]  = "Firm Id";
                csvHeaders[1]  = "Acct Id";
                csvHeaders[2]  = "Exchange";
                csvHeaders[3]  = "Ticker Symbol";
                csvHeaders[4]  = "Product Name";
                csvHeaders[5]  = "CC Code";
                csvHeaders[6]  = "Period Code";
                csvHeaders[7]  = "Put / Call";
                csvHeaders[8]  = "Strike";
                csvHeaders[9]  = "Underlying Period Code";
                csvHeaders[10] = "Net Positions";
                csvHeaders[11] = "Margin Type";
                csvHeaders[12] = "Client ID";

                string[][] output = new string[2][];
                output[0] = csvHeaders;
                //String[] line = { "test","te","CBT","ZNU4","10Y TREASURY NOTE FUTURES","21","201409","","","","1","" };
                //output[1] = line;

                String[] line1 = { "test10", "2", "CME", "6EU4", "", "EC", "201409", "", "", "", "1", "" };
                output[1] = line1;

                //String[] line2 = { "tml1", "2", "CBT", "OZNN4 P1230", "", "21", "201407", "PUT", "123", "201409", "1", "" };
                //output[1] = line2;

                int length = output.GetLength(0);

                StringBuilder stringVal = new StringBuilder();

                //stringVal.Clear();
                for (int index = 0; index < length; index++)
                {
                    string lineInput;

                    if (index == 0)
                    {
                        lineInput = "\"" + string.Join("\",\"", output[index]) + "\"";
                    }
                    else
                    {
                        lineInput = "\"" + string.Join("\",\"", output[index]) + "\"" + ",";
                    }

                    stringVal.Append(lineInput);

                    stringVal.Append("\n");

                    //stringVal.Append(",");
                }

                Console.Write(stringVal.ToString());

                File.WriteAllText("D:/test.csv", stringVal.ToString());



                //StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriterWithEncoding(Encoding.UTF8);

                //sw. = Encoding.UTF8;



                // XmlWriterSettings xws = new XmlWriterSettings();

                //Encoding utf8noBOM = new UTF8Encoding();
                //xws.Encoding = Encoding.UTF8;

                //sw.Encoding = utf8noBOM;

                XmlWriter xw = XmlWriter.Create(sw);
                //xw.Settings.Encoding = new UTF8Encoding();
                xw.WriteStartDocument(true);

                //<core:marginReq xmlns:core="http://cmegroup.com/schema/core/1.1">

                xw.WriteStartElement("ns2", "portfolioReq", "http://cmegroup.com/schema/core/1.2");

                //xw.WriteAttributes

                //xw.WriteAttributeString("marginReq", "xmlns", "http://cmegroup.com/schema/core/1.1");

                xw.WriteStartElement("portfolio");

                xw.WriteAttributeString("desc", "My Desc");
                xw.WriteAttributeString("name", "My Name");
                xw.WriteAttributeString("rptCcy", "USD");
                xw.WriteAttributeString("id", "9");

                xw.WriteStartElement("entities");
                xw.WriteAttributeString("clrMbrFirmId", "My Firm");
                xw.WriteAttributeString("custAcctId", "My Account");

                xw.WriteEndElement();

                //xw.WriteStartElement("transactions");

                //xw.WriteStartElement("transaction");

                //xw.WriteAttributeString("type", "TRADE");
                //xw.WriteAttributeString("id", "0");

                //xw.WriteStartElement("payload");
                //xw.WriteAttributeString("encoding", "STRING");
                //xw.WriteAttributeString("format", "CSV");

                //xw.WriteStartElement("string");
                //xw.WriteString(stringVal.ToString());

                //xw.WriteEndElement();

                //xw.WriteEndElement(); //transactions

                //xw.WriteEndElement();

                //xw.WriteEndElement();

                //xw.WriteStartElement("amounts");

                //xw.WriteAttributeString("ccy", "USD");
                //xw.WriteAttributeString("conc", "0.0");
                //xw.WriteAttributeString("init", "0.0");
                //xw.WriteAttributeString("maint", "0.0");
                //xw.WriteAttributeString("nonOptVal", "0.0");
                //xw.WriteAttributeString("optVal", "0.0");



                xw.WriteEndDocument();
                xw.Close();

                Console.WriteLine(sw.ToString());

                //sb.Replace("utf-16", "UTF-8");

                //string finalXMLstring = Encoding.UTF8.GetString();

                //string xmlX = ""

                //string myObjectAsXml = SerializationHelper.SerializeEntity<T>(targetObject);

                //XmlSerializer x = new XmlSerializer(typeof(Margin));

                //StringWriter sww = new StringWriter();
                // XmlWriter writer = XmlWriter.Create(sww);
                // //xsSubmit.Serialize(writer, subReq);

                //x.Serialize(writer, margin);

                //HttpResponseMessage response = await client.GetAsync("portfolios/test1");

                ////response.EnsureSuccessStatusCode();

                ////string x = response.Headers.Select("sessionId");

                //if (response.IsSuccessStatusCode)
                //{
                //    String x = await response.Content.ReadAsStringAsync();

                //    Console.WriteLine(x);
                //}

                String test = sw.ToString();  //.Substring(38);
                Console.WriteLine("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
                Console.WriteLine(test);

                HttpResponseMessage response = await client.PostAsXmlAsync("portfolios", test);

                TSErrorCatch.debugWriteOut(test);

                //HttpResponseMessage response = await client.PostAsXmlAsync("margins/complete=true", xml);

                //client.GetAsync("margins/sp");
                //HttpResponseMessage response = await client.GetAsync("portfolios");

                //Task<string> response = await client.GetStringAsync("portfolios/").Result;

                //Task<HttpResponseMessage> getStringTask = client.PostAsync("https://cmecorenr.cmegroup.com/MarginServiceApi/margins", null);

                //HttpResponseMessage response = await getStringTask;

                //HttpResponseMessage response = await client.GetAsync("/margins/sp");

                //var jsonString = await client.GetStringAsync(portfoliosHttp);

                //TSErrorCatch.debugWriteOut(jsonString);

                if (response.IsSuccessStatusCode)
                {
                    //Product product = await response.Content.ReadAsAsync>Product>();
                    //Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);

                    String x = await response.Content.ReadAsStringAsync();

                    Console.WriteLine(x);
                }
            }
        }