ASP.NET MVC - 使用 cURL 或类似方法在应用程序中执行请求 [英] ASP.NET MVC - Using cURL or similar to perform requests in application

查看:17
本文介绍了ASP.NET MVC - 使用 cURL 或类似方法在应用程序中执行请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 ASP.NET MVC 中构建应用程序(使用 C#),我想知道如何执行 curl http://www.mywebsite.com/clients_list.xml 在我的控制器中基本上我想构建一种 REST API 来执行诸如显示编辑和删除之类的操作,例如 Twitter API.

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 Basically I would like to build a kind of REST API to perform actions such as show edit and delete, such as Twitter API.

但不幸的是,直到现在我在这个网站上除了那个用于 windows 的 cURL 之外没有找到任何东西:http://curl.haxx.se/

But unfortunately until now I didn't find anything besides that cURL for windows on this website: http://curl.haxx.se/

所以我不知道是否有任何传统方法可以使用 post delete 和 put on the requests 等方法从 URL 检索这种调用...

So I don't know if is there any traditional way to retrieve this kind of call from URL with methods like post delete and put on the requests, etc...

我只想知道一种简单的方法来在我的 ASP.NET MVC 应用程序的控制器中执行 curl 等命令.

I just would like to know an easy way to perform commands like curl inside my controller on my ASP.NET MVC Application.

更新:

所以我设法发出 GET 请求,但现在我在检索 POST 请求时遇到了严重的问题,例如,我正在使用来自 Twitter 的更新状态 API,它在 curl 中的工作方式如下:

Hi so I managed to make GET Requests but now I'm having a serious problem in retrieve POST Request for example, I'm using the update status API from Twitter that in curl would work like this:

curl -u user:password -d "status=playing with cURL and the Twitter API" http://twitter.com/statuses/update.xml

但是在我的 ASP.NET MVC 应用程序中,我在我的自定义函数中是这样做的:

but on my ASP.NET MVC application I'm doing like this inside my custom function:

string responseText = String.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
request.Method = "POST";
request.Credentials = new NetworkCredential("username", "password");
request.Headers.Add("status", "Tweeting from ASP.NET MVC C#");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    responseText = sr.ReadToEnd();
}
return responseText;

现在的问题是这个请求返回了403 Forbidden,我真的不知道为什么它在 curl 上完美运行

Now the problem is that this request is returning 403 Forbidden, I really don't know why if it works perfectly on curl

:\

更新:

我终于设法让它工作了,但可能有一种方法可以让它更干净漂亮,因为我是 C# 新手,我需要更多的知识来做到这一点,POST 参数的传递方式让我非常很困惑,因为很多代码只是传递参数.

I finally manage to get it working, but probably there's a way to make it cleaner and beautiful, as I'm new on C# I'll need more knowledge to do it, the way the POST params are passed makes me very confused because is a lot of code to just pass params.

好吧,我已经创建了一个 Gist - http://gist.github.com/215900 ,所以每个人都可以随意修改它.感谢您的帮助çağdaş

Well, I've created a Gist - http://gist.github.com/215900 , so everybody feel free to revise it as you will. Thanks for your help çağdaş

也按照这里的代码:

public string TwitterCurl()
{
    //PREVENT RESPONSE 417 - EXPECTATION FAILED
    System.Net.ServicePointManager.Expect100Continue = false;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
    request.Method = "POST";
    request.Credentials = new NetworkCredential("twitterUsername", "twitterPassword");

    //DECLARE POST PARAMS
    string headerVars = String.Format("status={0}", "Tweeting from ASP.NET MVC C#");
    request.ContentLength = headerVars.Length;

    //SEND INFORMATION
    using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream(), ASCIIEncoding.ASCII))
    {
        streamWriter.Write(headerVars);
        streamWriter.Close();
    }

    //RETRIEVE RESPONSE
    string responseText = String.Empty;
    using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream()))
    {
        responseText = sr.ReadToEnd();
    }

    return responseText;

    /*
    //I'M NOT SURE WHAT THIS IS FOR            
        request.Timeout = 500000;
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Custom Twitter Agent";
        #if USE_PROXY
            request.Proxy = new WebProxy("http://localhost:3000", false);
        #endif
    */
}

推荐答案

尝试使用 Microsoft.Http.HttpClient.这就是您的请求的样子

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();

您可以在 WCF REST Starter kit Preview 中找到此库及其源2,但是它可以独立于那里的其他东西使用.

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.

附言我在我的 twitter 帐户上测试了这段代码,它有效.

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

这篇关于ASP.NET MVC - 使用 cURL 或类似方法在应用程序中执行请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆