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#???

6
  • Are you trying to invoke CURL, or do you simply want to issue a web request? Commented May 26, 2016 at 13:24
  • i want to make web request with those parameters and it should receive a valid response Commented May 26, 2016 at 13:25
  • @Mounarajan, I think you should reframe the question. Your comment above is a better description of what you want to do. Commented May 26, 2016 at 14:06
  • Done @SilentMonk do you know answer? Commented May 26, 2016 at 14:09
  • @Thomas none of the answers there shows how to set all the required headers and cookies. Commented May 26, 2016 at 14:39

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Certainly works, but it's not the recommended class in the current version of .Net (4.5).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.