我在这个网站上看到很多例子显示webrequest相当于curl,但是,因为我完全没有PHP体验,所以它仍让我目瞪口呆。
有人可以告诉我这个curl命令的webrequest等价物:
curl -H "Content-Type:application/json" -X POST -d '{"name":"download_casestudy_a","casestudy":"A","type":"trackLink","href":"http://www.example.com","key":"your_key","session_id":"f33234de-cc75-4f28-9e9a-afb0014a5daf"}' https://in-automate.sendinblue.com/p
答案 0 :(得分:2)
使用您的给定选项和数据migth发送请求如下所示:
static void Main(string[] args)
{
var request = WebRequest.Create(new Uri("https://in-automate.sendinblue.com/p"));
var json =
"'{'name':'download_casestudy_a','casestudy':'A','type':'trackLink','href':'http://www.example.com','key':'your_key','session_id':'f33234de-cc75-4f28-9e9a-afb0014a5daf'}'";
request.Headers.Add("Content-Type", "application/json");
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(stream: httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}