• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTIxMQ==

Related Code
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
UnitOfWork Action filter for ASP.NET MVC and nHibernate
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
ASP.NET MVC 4.0 Web API Message logger for .Net 4.0
Generic Entity Framework 4.0 Base Repository
ForEach Extension method for IEnumerable<T>
Invoke Extension Method
Use of Extension methods to hide "infrastructure code"
PagedList class and paging code for ASP.Net MVC (Nothing new but kind of usefull.)
Null Dot "Operator" Extension Method
Fork of GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6
Generic Entity Framework 4.0 Base Repository with Paging
Google Analytics helper for ASP.NET Razor
Unity Controller Factory for ASP.NET MVC 3.0
Generic linq-to-sql repository
WCF RIA Services Unity 2.0 DomainService Factory
ASP.NET MVC 4.0 Web API Message logger for .Net 4.5
A Default Entity Framework 4.0 ObjectContext Factory
StringFormat Extension method for HtmlHelper ASP.NET MVC
Custom C# exception class
Get Cookies in Web Api Asp .net Mvc 4 RC
Email validation with Regular Expression
GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6
Reflection dynamically get private fields or properties
Url Validation with Regular Expression

Extension to the HttpClient

Extension method to simplify posting with HttpClient, also an sync extension method.

0
788 0 0 0 0 0

public static class HttpClientExtenstion
{
    public static HttpResponseMessage Post(
                                             this HttpClient httpClient,
                                             string requestUri,
                                             string data,
                                             string mediaType = "application/json")
    {
        var request = new HttpRequestMessage(HttpMethod.Post, requestUri)
        {
            Content = new StringContent(data, Encoding.UTF8, mediaType)
        };

        return httpClient.SendAsync(request).Result;
    }


    public static Task<HttpResponseMessage> PostAsync(
                                                     this HttpClient httpClient,
                                                     string requestUri,
                                                     string data,
                                                     string mediaType = "application/json")
    {
        var request = new HttpRequestMessage(HttpMethod.Post, requestUri)
        {
            Content = new StringContent(data, Encoding.UTF8, mediaType)
        };

        return httpClient.SendAsync(request);
    }


    public static Task<HttpResponseMessage> PostAsync<T>(
                                                        this HttpClient httpClient,
                                                        string requestUri,
                                                        T data,
                                                        string mediaType = "application/json")
    {
        var request = new HttpRequestMessage<T>(data, mediaType)
        {
            RequestUri = new Uri(requestUri),
            Method = HttpMethod.Post
        };

        return httpClient.SendAsync(request);
    }
}

Can be used in the following way:

var client = new HttpClient();

string contact = "{ \"ContactId\" : 1, \"Name\" : \"John Doe\"}";

var result = client.PostAsync("http://localhost:9090/api/contact", contact).Result;

Share: twitter | facebook   Action: used | fork | flag

.net

Mark '.net' tag as 'like'

Mark '.net' tag as 'ignore'

aspmvc

Mark 'aspmvc' tag as 'like'

Mark 'aspmvc' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

webapi

Mark 'webapi' tag as 'like'

Mark 'webapi' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
February 16, 2012 5:50 PM

Fork

 Extension to the HttpClient -  @fredrikn Thursday 16, 2012 5:50 PM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010