5

How do i make a curl request in c# in windows or

i want to make web request with this parameters and it should receive a valid response

request

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

In perl i would simply do

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`;

Then

my $page

The results are store in above variable.

How to do similarly in c#???

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Mounarajan
  • 1,357
  • 5
  • 22
  • 43

2 Answers2

9

I would highly recommend using the new HttpClient.

Please read the notes at the bottom of this answer

Excerpt from 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);
 }

Since this answer was originally written there are some caveats about using HttpClient. (TLDR; it should be a singleton)

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

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0

Use HttpWebRequest E.g.

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

You can set headers/cookies on request by accessing Headers and CookieContainer properties of request object. You can also access various properties of response object to get various values.

loopedcode
  • 4,863
  • 1
  • 21
  • 21