当前位置: 首页>>代码示例>>C#>>正文


C# RestClient.ExecuteAsPost方法代码示例

本文整理汇总了C#中RestSharp.RestClient.ExecuteAsPost方法的典型用法代码示例。如果您正苦于以下问题:C# RestClient.ExecuteAsPost方法的具体用法?C# RestClient.ExecuteAsPost怎么用?C# RestClient.ExecuteAsPost使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RestSharp.RestClient的用法示例。


在下文中一共展示了RestClient.ExecuteAsPost方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SimpleInjectionViaStringAsJson

        /// <summary>
        /// This example uses the raw JSON string to create a POST
        /// request for sending one or more SMTP messages through Email-On-Demand.
        /// 
        /// The JSON request generated by this sample code looks like this:
        /// 
        ///{
        ///    "ServerId": "YOUR SERVER ID HERE",
        ///    "ApiKey": "YOUR API KEY HERE",
        ///    "Messages": [{
        ///        "Subject": "Email subject line for raw JSON example.",
        ///        "TextBody": "The text portion of the message.",
        ///        "HtmlBody": "<h1>The html portion of the message</h1><br/>Test",
        ///        "To": [{
        ///            "EmailAddress": "[email protected]",
        ///            "FriendlyName": "Customer Name"
        ///        }],
        ///        "From": {
        ///            "EmailAddress": "[email protected]",
        ///            "FriendlyName": "From Address"
        ///        },
        ///    }]
        ///}
        /// </summary>
        public static void SimpleInjectionViaStringAsJson(
            int serverId, string yourApiKey, string apiUrl)
        {
            // The client object processes requests to the SocketLabs Injection API.
            var client = new RestClient(apiUrl);

            // Construct the string used to generate JSON for the POST request.
            string stringBody =
                "{" +
                    "\"ServerId\":\"" + serverId + "\"," +
                    "\"ApiKey\":\"" + yourApiKey + "\"," +
                    "\"Messages\":[{" +
                        "\"Subject\":\"Email subject line for raw JSON example.\"," +
                        "\"TextBody\":\"The text portion of the message.\"," +
                        "\"HtmlBody\":\"<h1>The html portion of the message</h1><br/>Test\"," +
                        "\"To\":[{" +
                            "\"EmailAddress\":\"[email protected]\"," +
                            "\"FriendlyName\":\"Customer Name\"" +
                        "}]," +
                        "\"From\":{" +
                            "\"EmailAddress\":\"[email protected]\"," +
                            "\"FriendlyName\":\"From Address\"" +
                        "}," +
                    "}]" +
                "}";

            try
            {
                // Generate a new POST request.
                var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json };

                // Store the request data in the request object.
                request.AddParameter("application/json; charset=utf-8", stringBody, ParameterType.RequestBody);

                // Make the POST request.
                var result = client.ExecuteAsPost(request, "POST");

                // Store the response result in our custom class.
                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result.Content)))
                {
                    var serializer = new DataContractJsonSerializer(typeof (PostResponse));
                    var resp = (PostResponse) serializer.ReadObject(stream);

                    // Display the results.
                    if (resp.ErrorCode.Equals("Success"))
                    {
                        Console.WriteLine("Successful injection!");
                    }
                    else
                    {
                        Console.WriteLine("Failed injection! Returned JSON is: ");
                        Console.WriteLine(result.Content);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error, something bad happened: " + ex.Message);
            }
        }
开发者ID:JoeSocketLabs,项目名称:email-on-demand-examples,代码行数:84,代码来源:JsonString.cs

示例2: DeleteContainerById

        public async Task DeleteContainerById(string hostId, string id)
        {
            Debug.Assert(id != null);
            var container = await GetContainerById(hostId, id);
            var host = await HostActor.GetHostById(container.HostId);

            // Remove container from Docker
            var restClient = new RestClient(host.HostUrl);
            var request = new RestRequest(string.Format("/containers/{0}", id));

            var response = restClient.ExecuteAsPost(request, "DELETE");
            // TODO - check response for success
        }
开发者ID:gugrosbo,项目名称:POC,代码行数:13,代码来源:ContainerActor.cs

示例3: QueueLog

        private void QueueLog(LogMessage message)
        {
            _loggingEvents.Enqueue(message);

            var client = new RestClient(postUrl);
            var request = new RestRequest();
            request.Resource = "api/Logger";
            request.AddJsonBody(message);

            client.ExecuteAsPost(request, "POST");

            var temp = 1;
        }
开发者ID:chdev77,项目名称:Rest4Log4Net,代码行数:13,代码来源:Appender.cs

示例4: ToAHDC

        public static void ToAHDC(DTO.LabOrder order, string username, string password)
        {
            // Written by Scott Ross
            // Cornell University 2013
            // Using REST Sharp, attach to our webserivces, send message
            // Serialization and Deserialization using REST Sharp

            //Instainaite the Rest Client using base url
            var client = new RestClient("http://localhost:54490/");

            //Create the rest request uing the oepration (in this case, we are posting a lab order
            var request = new RestRequest("labOrder/?format=xml", Method.POST);           

            //Serialize Order
            RestSharp.Serializers.DotNetXmlSerializer serialize = new RestSharp.Serializers.DotNetXmlSerializer();
            String msg = serialize.Serialize(order).ToString();

            //Add msg to the parametertype of request body.  
            request.AddParameter("text/xml", msg, ParameterType.RequestBody);
            request.AddHeader("username", username);
            request.AddHeader("password", password);            

            //Execute & access "content"   
            //Access the message in response
            //The REsponse XML looks like:
            var res = client.ExecuteAsPost(request, "Post");
            res.ContentType = "application/xml";

            /*
            RestSharp.Deserializers.XmlDeserializer deserial = new RestSharp.Deserializers.XmlDeserializer();
            var response = deserial.Deserialize<Response_DTO.LabOrderResponse>(res);

            if (response.Ack.Status.Equals("AA"))
            {
                Console.WriteLine("Successfull Sending of Order!");
            }
            else
            {
                Console.WriteLine(response.Ack.ErrorDetails);
            }
             * */
        }
开发者ID:sross07,项目名称:AHDC.External.REST_Example,代码行数:42,代码来源:SendOrder.cs

示例5: SimpleInjectionViaSdkObjectAsJson

        /// <summary>
        /// This example uses the SocketLabs SDK PostBody object to create a POST
        /// request for sending one or more SMTP messages through Email-On-Demand.
        /// The SocketLabs SDK library is required, and for this example we also use
        /// the RestSharp library to ask as our serializer and client.
        /// 
        /// The JSON request generated by this sample code looks like this:
        /// 
        ///{
        ///    "ServerId": "YOUR SERVER ID HERE",
        ///    "ApiKey": "YOUR API KEY HERE",
        ///    "Messages": [{
        ///        "MailingId": null,
        ///        "MessageId": null,
        ///        "MergeData": null,
        ///        "Subject": "Email subject line for SDK example.",
        ///        "TextBody": "The text portion of the message.",
        ///        "HtmlBody": "<h1>The HTML portion of the message</h1><br/><p>A paragraph.</p>",
        ///        "CustomHeaders": null,
        ///        "To": [{
        ///            "EmailAddress": "[email protected]",
        ///            "FriendlyName": null
        ///        }],
        ///        "Cc": null,
        ///        "Bcc": null,
        ///        "From": {
        ///            "EmailAddress": "[email protected]",
        ///            "FriendlyName": "From Address"
        ///        },
        ///        "ReplyTo": null,
        ///        "Charset": null,
        ///        "Attachments": null
        ///    }]
        ///}
        /// </summary>
        public static void SimpleInjectionViaSdkObjectAsJson(
            int serverId, string yourApiKey, string apiUrl)
        {
            // The client object processes requests to the SocketLabs Injection API.
            var client = new RestClient(apiUrl);

            // Construct the object used to generate JSON for the POST request.
            var postBody = new PostBody
            {
                ServerId = serverId,
                ApiKey = yourApiKey,
                Messages = new[]
                {
                    new EmailMessage
                    {
                        Subject = "Email subject line for SDK example.",
                        To = new[]
                        {
                            new Address
                            {
                                EmailAddress = "[email protected]",
                                FriendlyName = null
                            }
                        },
                        From = new Address
                        {
                            EmailAddress = "[email protected]",
                            FriendlyName = "From Address"
                        },
                        TextBody = "The text portion of the message.",
                        HtmlBody = "<h1>The HTML portion of the message</h1><br/><p>A paragraph.</p>",
                    }
                }
            };

            try
            {
                // Generate a new POST request.
                var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json };

                // Store the request data in the request object.
                request.AddBody(postBody);

                // Make the POST request.
                var result = client.ExecuteAsPost<PostResponse>(request, "POST");

                // Store the response result in our custom class.
                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result.Content)))
                {
                    var serializer = new DataContractJsonSerializer(typeof (PostResponse));
                    var resp = (PostResponse) serializer.ReadObject(stream);

                    // Display the results.
                    if (resp.ErrorCode.Equals("Success"))
                    {
                        Console.WriteLine("Successful injection!");
                    }
                    else
                    {
                        Console.WriteLine("Failed injection! Returned JSON is: ");
                        Console.WriteLine(result.Content);
                    }
                }
            }
            catch (Exception ex)
//.........这里部分代码省略.........
开发者ID:JoeSocketLabs,项目名称:email-on-demand-examples,代码行数:101,代码来源:JsonSdkObject.cs

示例6: SimpleInjectionApiTemplate

        /// <summary>
        /// This example uses generic JsonObject containers to create a POST
        /// request for sending one or more SMTP messages through Email-On-Demand.
        /// 
        /// The JSON request generated by this sample code looks like this:
        /// 
        ///{
        ///    "ServerId": "YOUR SERVER ID HERE",
        ///    "ApiKey": "YOUR API KEY HERE",
        ///    "Messages": [{
        ///        "Subject": "API Template Example",
        ///        "ApiTemplate": "1",
        ///        "To": [{
        ///            "EmailAddress": "%%DeliveryAddress%%",
        ///            "FriendlyName": "%%FirstName"
        ///        }],
        ///        "From": {
        ///            "EmailAddress": "[email protected]",
        ///            "FriendlyName": "The ABC Company"
        ///        },
        ///        "MergeData": {
        ///             "PerMessage": [
        ///                 [
        ///                     {
        ///                         "Field":"DeliveryAddress",
        ///                          "Value":"[email protected]"
        ///                     },
        ///              	    {                  
        ///                         "Field": "FirstName",
        ///                         "Value": "John"
        ///                     }
        ///                 ]
        ///             ]
        ///         }
        ///     }] 
        ///}
        /// </summary>
        public static void SimpleInjectionApiTemplate(
            int serverId, string yourApiKey, string apiUrl)
        {
            // The client object processes requests to the SocketLabs Injection API.
            var client = new RestClient(apiUrl);

            // Construct the objects used to generate JSON for the POST request.
            var recipient1 = new JsonObject();
            recipient1.Add("EmailAddress", "%%DeliveryAddress%%");
            recipient1.Add("FriendlyName", "%%FirstName%%");

            var toList = new JsonArray();
            toList.Add(recipient1);

            var fromField = new JsonObject();
            fromField.Add("EmailAddress", "[email protected]");
            fromField.Add("FriendlyName", "The ABC Company");

            //The DeliveryAddress field is required, all other merge fields are optional.
            //If your template contains merge fields, you must specify them here.
            var mergeDeliveryAddress = new JsonObject();
            mergeDeliveryAddress.Add("Field", "DeliveryAddress");
            mergeDeliveryAddress.Add("Value", "[email protected]");

            var mergeFirstName = new JsonObject();
            mergeFirstName.Add("Field", "FirstName");
            mergeFirstName.Add("Value", "John");

            var mergeRecipient1 = new JsonArray();
            mergeRecipient1.Add(mergeDeliveryAddress);
            mergeRecipient1.Add(mergeFirstName);

            var perMessage = new JsonArray();
            perMessage.Add(mergeRecipient1);

            var mergeData = new JsonObject();
            mergeData.Add("PerMessage", perMessage);

            var message1 = new JsonObject();
            message1.Add("Subject", "API Template Example");
            message1.Add("ApiTemplate", "1"); //Template ID found in the SocketLabs On-Demand Control Panel
            message1.Add("To", toList);
            message1.Add("From", fromField);
            message1.Add("MergeData", mergeData);

            var messageArray = new JsonObject[1];
            messageArray[0] = message1;

            var body = new JsonObject();
            body.Add("ServerId", serverId);
            body.Add("ApiKey", yourApiKey);
            body.Add("Messages", messageArray);

            try
            {
                // Generate a new POST request.
                var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json };

                // Store the request data in the request object.
                request.AddBody(body);

                // Make the POST request.
                var result = client.ExecuteAsPost(request, "POST");
//.........这里部分代码省略.........
开发者ID:socketlabs,项目名称:email-on-demand-examples,代码行数:101,代码来源:JsonGenericApiTemplate.cs

示例7: SimpleInjectionViaRestSharpAsJson

        /// <summary>
        /// This example uses generic JsonObject containers to create a POST
        /// request for sending one or more SMTP messages through Email-On-Demand.
        /// 
        /// The JSON request generated by this sample code looks like this:
        /// 
        ///{
        ///    "ServerId": "YOUR SERVER ID HERE",
        ///    "ApiKey": "YOUR API KEY HERE",
        ///    "Messages": [{
        ///        "Subject": "Email subject line for generic object example.",
        ///        "TextBody": "The text portion of the message.",
        ///        "HtmlBody": "<h1>The HTML portion of the message</h1><br/><p>A paragraph.</p>",
        ///        "To": [{
        ///            "EmailAddress": "[email protected]",
        ///            "FriendlyName": "Customer Name"
        ///        }],
        ///        "From": {
        ///            "EmailAddress": "[email protected]",
        ///            "FriendlyName": "The ABC Company"
        ///        }
        ///    }]
        ///}
        /// </summary>
        public static void SimpleInjectionViaRestSharpAsJson(
            int serverId, string yourApiKey, string apiUrl)
        {
            // The client object processes requests to the SocketLabs Injection API.
            var client = new RestClient(apiUrl);

            // Construct the objects used to generate JSON for the POST request.
            var recipient1 = new JsonObject();
            recipient1.Add("EmailAddress", "[email protected]");
            recipient1.Add("FriendlyName", "Customer Name");

            var toList = new JsonObject[1];
            toList[0] = recipient1;

            var fromField = new JsonObject();
            fromField.Add("EmailAddress", "[email protected]");
            fromField.Add("FriendlyName", "The ABC Company");

            var message1 = new JsonObject();
            message1.Add("Subject", "Email subject line for generic object example.");
            message1.Add("TextBody", "The text portion of the message.");
            message1.Add("HtmlBody", "<h1>The HTML portion of the message</h1><br/><p>A paragraph.</p>");
            message1.Add("To", toList);
            message1.Add("From", fromField);

            var messageArray = new JsonObject[1];
            messageArray[0] = message1;

            var body = new JsonObject();
            body.Add("ServerId", serverId);
            body.Add("ApiKey", yourApiKey);
            body.Add("Messages", messageArray);

            try
            {
                // Generate a new POST request.
                var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json };

                // Store the request data in the request object.
                request.AddBody(body);

                // Make the POST request.
                var result = client.ExecuteAsPost(request, "POST");

                // Store the response result in our custom class.
                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result.Content)))
                {
                    var serializer = new DataContractJsonSerializer(typeof (PostResponse));
                    var resp = (PostResponse) serializer.ReadObject(stream);

                    // Display the results.
                    if (resp.ErrorCode.Equals("Success"))
                    {
                        Console.WriteLine("Successful injection!");
                    }
                    else
                    {
                        Console.WriteLine("Failed injection! Returned JSON is: ");
                        Console.WriteLine(result.Content);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error, something bad happened: " + ex.Message);
            }
        }
开发者ID:JoeSocketLabs,项目名称:email-on-demand-examples,代码行数:91,代码来源:JsonGenericObject.cs

示例8: CompleteInjectionViaSdkObjectAsJson


//.........这里部分代码省略.........
            };

            // The second of two messages to be injected; this example shows how
            // to use the inline Merge feature, which customizes a message for its
            // recipients.
            postBody.Messages[1] = new EmailMessage
            {
                Subject = "Employee mentorship program information.",
                To = new[]
                {
                    // DeliveryAddress is required for all inline merges. Additionally, any other addresses
                    // used in the To, Cc, or Bcc fields will be for header display only, and will not receive
                    // the injected email. Only the DeliveryAddress members will receive the injected message.
                    new Address
                    {
                        EmailAddress = "%%DeliveryAddress%%",
                        FriendlyName = "%%RecipientName%%"
                    }
                },
                From = new Address
                {
                    EmailAddress = "[email protected]",
                    FriendlyName = "HR Department"
                },
                MailingId = "%%MailingId%%",
                // Merge field variables can go anywhere else in the message. A basic use would be to define
                // a unique MessageId for each outgoing message.
                MessageId = "%%CUSTOM-SERIAL-NUMBER-ID%%",
                // We can even reuse the same variable multiple times, such as %%RecipientName%% being used both
                // to define the name of the recipient in the header, and for addressing the recipient in the
                // message body.
                TextBody = "%%RecipientName%%, you have been paired with %%SeniorEmployeeName%% as part of our mentorship program!",
                HtmlBody = "<h1>%%RecipientName%%, you have been paired with %%SeniorEmployeeName%% as part of our mentorship program!</h1>",
            };

            // Values for each of the merge data fields need to be added.
            postBody.Messages[1].MergeData = new MergeData();

            // Global variables apply to all recipients in the same way.
            postBody.Messages[1].MergeData.Global = new MergeRow[1];
            postBody.Messages[1].MergeData.Global[0] = new MergeRow("MailingId", "Human Resources 018");

            // Per-Message variables are unique to each recipient.
            postBody.Messages[1].MergeData.PerMessage = new MergeRow[3][];

            // Recipient 1 of 3
            postBody.Messages[1].MergeData.PerMessage[0] = new MergeRow[4];
            postBody.Messages[1].MergeData.PerMessage[0][0] = new MergeRow("DeliveryAddress", "[email protected]");
            postBody.Messages[1].MergeData.PerMessage[0][1] = new MergeRow("RecipientName", "Robert Smith");
            postBody.Messages[1].MergeData.PerMessage[0][2] = new MergeRow("SeniorEmployeeName", "Elizabeth Johnson");
            postBody.Messages[1].MergeData.PerMessage[0][3] = new MergeRow("CUSTOM-SERIAL-NUMBER-ID", "0000-0000-0000-8888-0001");

            // Recipient 2 of 3
            postBody.Messages[1].MergeData.PerMessage[1] = new MergeRow[4];
            postBody.Messages[1].MergeData.PerMessage[1][0] = new MergeRow("DeliveryAddress", "[email protected]");
            postBody.Messages[1].MergeData.PerMessage[1][1] = new MergeRow("RecipientName", "Alice White");
            postBody.Messages[1].MergeData.PerMessage[1][2] = new MergeRow("SeniorEmployeeName", "Elizabeth Johnson");
            postBody.Messages[1].MergeData.PerMessage[1][3] = new MergeRow("CUSTOM-SERIAL-NUMBER-ID", "0000-0000-0000-8888-0002");

            // Recipient 3 of 3
            postBody.Messages[1].MergeData.PerMessage[2] = new MergeRow[4];
            postBody.Messages[1].MergeData.PerMessage[2][0] = new MergeRow("DeliveryAddress", "[email protected]");
            postBody.Messages[1].MergeData.PerMessage[2][1] = new MergeRow("RecipientName", "Christine Johnson");
            postBody.Messages[1].MergeData.PerMessage[2][2] = new MergeRow("SeniorEmployeeName", "Gary Brown");
            postBody.Messages[1].MergeData.PerMessage[2][3] = new MergeRow("CUSTOM-SERIAL-NUMBER-ID", "0000-0000-0000-8888-0003");

            try
            {
                // Generate a new POST request.
                var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json };

                // Store the request data in the request object.
                request.AddBody(postBody);

                // Make the POST request.
                var result = client.ExecuteAsPost(request, "POST");

                // Store the response result in our custom class.
                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(result.Content)))
                {
                    var serializer = new DataContractJsonSerializer(typeof (PostResponse));
                    var resp = (PostResponse) serializer.ReadObject(stream);

                    // Display the results.
                    if (resp.ErrorCode.Equals("Success"))
                    {
                        Console.WriteLine("Successful injection!");
                    }
                    else
                    {
                        Console.WriteLine("Failed injection! Returned JSON is: ");
                        Console.WriteLine(result.Content);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error, something bad happened: " + ex.Message);
            }
        }
开发者ID:rcoopman,项目名称:email-on-demand-examples,代码行数:101,代码来源:JsonSdkObjectComplete.cs


注:本文中的RestSharp.RestClient.ExecuteAsPost方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。