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

前端 未结 4 1942
一整个雨季
一整个雨季 2021-02-01 11:06

I\'m building an application in ASP.NET MVC (using C#) and I would like to know how I can perform calls like curl http://www.mywebsite.com/clients_list.xml inside my controller

4条回答
  •  再見小時候
    2021-02-01 11:44

    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.

提交回复
热议问题