ASP.NET MVC - Using cURL or similar to perform requests in application ASP.NET MVC - Using cURL or similar to perform requests in application curl curl

ASP.NET MVC - Using cURL or similar to perform requests in application


Try using Microsoft.Http.HttpClient. This is what your request would look like

var client = new HttpClient();client.DefaultHeaders.Authorization = Credential.CreateBasic("username","password");var form = new HttpUrlEncodedForm();form.Add("status","Test tweet using Microsoft.Http.HttpClient");var content = form.CreateHttpContent();var resp = client.Post("http://www.twitter.com/statuses/update.xml", content);string result = resp.Content.ReadAsString();

You can find this library and its source included in the WCF REST Starter kit Preview 2, however it can be used independently of the rest of the stuff in there.

P.S. I tested this code on my twitter account and it works.


Example code using HttpWebRequest and HttpWebResponse :

public string GetResponseText(string url) {    string responseText = String.Empty;    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);    request.Method = "GET";    HttpWebResponse response = (HttpWebResponse)request.GetResponse();    using (StreamReader sr = new StreamReader(response.GetResponseStream())) {        responseText = sr.ReadToEnd();    }    return responseText;}

To POST data :

public string GetResponseText(string url, string postData) {    string responseText = String.Empty;    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);    request.Method = "POST";    request.ContentLength = postData.Length;    using (StreamWriter sw = new StreamWriter(request.GetRequestStream())) {        sw.Write(postData);    }    HttpWebResponse response = (HttpWebResponse)request.GetResponse();    using (StreamReader sr = new StreamReader(response.GetResponseStream())) {        responseText = sr.ReadToEnd();    }    return responseText;}


This is the single line of code I use for calls to a RESTful API that returns JSON.

return ((dynamic) JsonConvert.DeserializeObject<ExpandoObject>(        new WebClient().DownloadString(            GetUri(surveyId))    )).data;

Notes

  • The Uri is generated off stage using the surveyId and credentials
  • The 'data' property is part of the de-serialized JSON object returnedby the SurveyGizmo API

The Complete Service

public static class SurveyGizmoService{    public static string UserName { get { return WebConfigurationManager.AppSettings["SurveyGizmo.UserName"]; } }    public static string Password { get { return WebConfigurationManager.AppSettings["SurveyGizmo.Password"]; } }    public static string ApiUri { get { return WebConfigurationManager.AppSettings["SurveyGizmo.ApiUri"]; } }    public static string SurveyId { get { return WebConfigurationManager.AppSettings["SurveyGizmo.Survey"]; } }    public static dynamic GetSurvey(string surveyId = null)    {        return ((dynamic) JsonConvert.DeserializeObject<ExpandoObject>(                new WebClient().DownloadString(                    GetUri(surveyId))            )).data;    }    private static Uri GetUri(string surveyId = null)    {        if (surveyId == null) surveyId = SurveyId;        return new UriBuilder(ApiUri)                {                        Path = "/head/survey/" + surveyId,                        Query = String.Format("user:pass={0}:{1}", UserName, Password)                }.Uri;    }}