简体   繁体   English

将 curl 转换为 http 请求并在 C# 中获取响应

[英]Converting curl to http request and getting the response in C#

I am new to C#, I want to make the following curl call in my C#(In perl i will directy use system call to mke curl request)我是 C# 新手,我想在我的 C# 中进行以下 curl 调用(在 perl 中,我将直接使用系统调用来 mke curl 请求)

curl 'http://shop.nordstrom.com/soajax/storeavailability?postalCode=90067&radius=100' -H 'Origin: http://shop.nordstrom.com' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -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 'Content-Type: application/json' -H 'Accept: application/json' -H 'Referer: http://shop.nordstrom.com/s/cece-by-cynthia-steffe-jackie-cold-shoulder-fit-flare-dress/4261930' --data-binary '{"SameDayDeliveryStoreNumber":0,"styleSkus":[{"StyleId":4261930,"SkuIds":[32882914,32877390,32877377,32882917,32882922,32882926,32877379,32882887,32882891,32877382,32882897,32882902,32882907,32882909]}],"RefreshSameDayDeliveryStore":true}' --compressed

First how could i change all the parameters to http so i can view the response in my browser首先我如何将所有参数更改为 http 以便我可以在浏览器中查看响应

Then Can I convert a cURL call to an HTTP request?那么我可以将 cURL 调用转换为 HTTP 请求吗? If so, how?如果是这样,如何? If not, how can I make the above cURL call from my C# program so i will get response properly?如果没有,我怎样才能从我的 C# 程序中进行上述 cURL 调用,以便我得到正确的响应?

Just make a HttpWebRequest :只需创建一个HttpWebRequest

var url = "http://shop.nordstrom.com/soajax/storeavailability?postalCode=90067&radius=100";
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Post;
request.Headers["origin"] = "http://shop.nordstrom.com";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Headers["Accept-Language"] = "en-US,en;q=0.8";
request.UserAgent = "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";
request.ContentType = "application/json";
request.Accept = "application/json";
request.Referer = "http://shop.nordstrom.com/s/cece-by-cynthia-steffe-jackie-cold-shoulder-fit-flare-dress/4261930";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write("{\"SameDayDeliveryStoreNumber\":0,\"styleSkus\":[{\"StyleId\":4261930,\"SkuIds\":[32882914,32877390,32877377,32882917,32882922,32882926,32877379,32882887,32882891,32877382,32882897,32882902,32882907,32882909]}],\"RefreshSameDayDeliveryStore\":true}");
}

var response = request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream()))
{
    var json = reader.ReadToEnd();
    // do stuffs...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM