【问题标题】:How make an HTTP request in C# [duplicate]如何在 C# 中发出 HTTP 请求 [重复]
【发布时间】:2016-05-26 13:22:56
【问题描述】:

我如何在 windows 中的 c# 中发出 curl 请求或

我想用这个参数发出网络请求,它应该会收到一个有效的响应

请求

curl 'http://www1.bloomingdales.com/api/store/v2/stores/367,363,6113,364,4946?upcNumber=808593890516' -H 'Cookie:shippingCountry=US;' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36' -H 'Accept-Language: en-US,en;q=0.8' -H 'Accept: application/json, text/javascript, */*; q=0.01' --compressed

在 perl 中我会这样做

my $page = `curl --silent 'http://www1.bloomingdales.com/api/store/v2/stores/367,363,6113,364,4946?upcNumber=808593890516' -H 'Cookie:shippingCountry=US;' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36' -H 'Accept-Language: en-US,en;q=0.8' -H 'Accept: application/json, text/javascript, */*; q=0.01' --compressed 2>/dev/null`;

然后

my $page

结果存储在上面的变量中。

如何在c#中做类似的事情???

【问题讨论】:

  • 您是在尝试调用 CURL,还是只是想发出 Web 请求?
  • 我想用这些参数发出网络请求,它应该收到一个有效的响应
  • @Mounarajan,我认为你应该重新提出这个问题。您上面的评论更好地描述了您想要做什么。
  • 完成@SilentMonk 你知道答案吗?
  • @Thomas 没有任何答案显示如何设置所有必需的标头和 cookie。

标签: c# curl


【解决方案1】:

我强烈推荐使用新的HttpClient

请阅读本答案底部的注释

摘自 MSDN。

static async void Main()
{

    // Create a New HttpClient object.
    HttpClient client = new HttpClient();

    // Call asynchronous network methods in a try/catch block to handle exceptions
    try 
    {
       HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
       response.EnsureSuccessStatusCode();
       string responseBody = await response.Content.ReadAsStringAsync();
       // Above three lines can be replaced with new helper method below
       // string responseBody = await client.GetStringAsync(uri);

       Console.WriteLine(responseBody);
    }  
    catch(HttpRequestException e)
    {
       Console.WriteLine("\nException Caught!");    
       Console.WriteLine("Message :{0} ",e.Message);
    }

    // Need to call dispose on the HttpClient object
    // when done using it, so the app doesn't leak resources
    client.Dispose(true);
 }

由于这个答案最初是写的,所以有一些关于使用 HttpClient 的警告。 (TLDR;应该是单例)

Using HttpClient As It Was Intended (Because You’re Not)

What is the overhead of creating a new HttpClient per call in a WebAPI client?

YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE

【讨论】:

    【解决方案2】:

    使用HttpWebRequest 例如

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.google.com");
    // access req.Headers to get/set header values before calling GetResponse. 
    // req.CookieContainer allows you access cookies.
    
    var response = req.GetResponse();
    string webcontent;
    using (var strm = new StreamReader(response.GetResponseStream()))
    {
        webcontent = strm.ReadToEnd();
    }
    

    您可以通过访问请求对象的HeadersCookieContainer 属性来根据请求设置标头/cookie。您还可以访问响应对象的各种属性以获取各种值。

    【讨论】:

    • 当然可以,但它不是当前版本的 .Net (4.5) 中推荐的类。
    猜你喜欢
    • 2019-04-19
    • 2017-05-16
    • 2021-10-26
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多