这个 Post webrequest 和 Postman 中的类似请求有什么区别?
What the difference between this Post webrequest and similar request in Postman?
我想向“https://sslecal2.forexprostools.com/ajax.php”发出 Post 请求。所以有我的代码:
string URI = "https://sslecal2.forexprostools.com/ajax.php";
string requestBody = String.Format("{{\"dateFrom\": \"{0}\", \"dateTo\": \"{1}\", \"timeZone\": {2}, \"action\": \"{3}\"}}",
"2018-12-24", "2018-12-24", 18, "filter"); //json format
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI); //make request
request.Method = "POST";
request.UserAgent = "";
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(requestBody); //write your request payload
}
WebResponse response = request.GetResponse();
string jsonData = String.Empty;
using (var reader = new StreamReader(response.GetResponseStream()))
{
jsonData = reader.ReadToEnd();
}
response.Close();
我在 "requestBody" in string " string requestBody = String.Format("{{\"dateFrom\"..." 因为我得到 200 和空 html回答。
我在邮递员中附加了相同请求的屏幕,并在回答中使用了 html 代码。邮递员中的此请求处理得很好。
这个 Post webrequest 和 Postman 中的请求有什么区别?
使用邮递员,您可以发布不同格式的数据。要在代码中获得相同的内容,您需要更改请求正文格式并设置请求的内容类型:
string URI = "https://sslecal2.forexprostools.com/ajax.php";
string requestBody = String.Format("dateFrom={0}&dateTo={1}&timeZone={2}&action={3}",
"2018-12-24", "2018-12-24", 18, "filter"); //<-- Change this
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI);
request.Method = "POST";
request.UserAgent = "";
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
request.ContentType = "application/x-www-form-urlencoded"; //<-- Add this
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(requestBody);
}
WebResponse response = request.GetResponse();
string jsonData = String.Empty;
using (var reader = new StreamReader(response.GetResponseStream()))
{
jsonData = reader.ReadToEnd();
}
response.Close();
在 PostMan 中,如果您单击右上角的 "Code",在发送按钮下,您可以选择 C# (RestSharp)。如果您不使用 RestSharp,则工作量很小将其转换为其他东西,但基础知识都在那里。
这是您的案例 (RestSharp) 的 autogen 输出:
var client = new RestClient("https://sslecal2.forexprostools.com/ajax.php");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "bfd1a3b3-983f-4160-a091-6f0962413e58");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("X-Requested-With", "XMLHttpRequest");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("undefined", "dateFrom=2018-01-24&dateTo=2018-01-24&timeZone=18&action=filter", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
将其转换为 HttpWebRequest 需要:
- 添加页眉 -> Headers.Add
- 指定方法
- 正文数据设置不同 - 获取 PostMan 的字符串并将其写入请求流
或从 NuGet 安装免费的 RestSharp
我想向“https://sslecal2.forexprostools.com/ajax.php”发出 Post 请求。所以有我的代码:
string URI = "https://sslecal2.forexprostools.com/ajax.php";
string requestBody = String.Format("{{\"dateFrom\": \"{0}\", \"dateTo\": \"{1}\", \"timeZone\": {2}, \"action\": \"{3}\"}}",
"2018-12-24", "2018-12-24", 18, "filter"); //json format
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI); //make request
request.Method = "POST";
request.UserAgent = "";
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(requestBody); //write your request payload
}
WebResponse response = request.GetResponse();
string jsonData = String.Empty;
using (var reader = new StreamReader(response.GetResponseStream()))
{
jsonData = reader.ReadToEnd();
}
response.Close();
我在 "requestBody" in string " string requestBody = String.Format("{{\"dateFrom\"..." 因为我得到 200 和空 html回答。
我在邮递员中附加了相同请求的屏幕,并在回答中使用了 html 代码。邮递员中的此请求处理得很好。
这个 Post webrequest 和 Postman 中的请求有什么区别?
使用邮递员,您可以发布不同格式的数据。要在代码中获得相同的内容,您需要更改请求正文格式并设置请求的内容类型:
string URI = "https://sslecal2.forexprostools.com/ajax.php";
string requestBody = String.Format("dateFrom={0}&dateTo={1}&timeZone={2}&action={3}",
"2018-12-24", "2018-12-24", 18, "filter"); //<-- Change this
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI);
request.Method = "POST";
request.UserAgent = "";
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
request.ContentType = "application/x-www-form-urlencoded"; //<-- Add this
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(requestBody);
}
WebResponse response = request.GetResponse();
string jsonData = String.Empty;
using (var reader = new StreamReader(response.GetResponseStream()))
{
jsonData = reader.ReadToEnd();
}
response.Close();
在 PostMan 中,如果您单击右上角的 "Code",在发送按钮下,您可以选择 C# (RestSharp)。如果您不使用 RestSharp,则工作量很小将其转换为其他东西,但基础知识都在那里。
这是您的案例 (RestSharp) 的 autogen 输出:
var client = new RestClient("https://sslecal2.forexprostools.com/ajax.php");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "bfd1a3b3-983f-4160-a091-6f0962413e58");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("X-Requested-With", "XMLHttpRequest");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("undefined", "dateFrom=2018-01-24&dateTo=2018-01-24&timeZone=18&action=filter", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
将其转换为 HttpWebRequest 需要:
- 添加页眉 -> Headers.Add
- 指定方法
- 正文数据设置不同 - 获取 PostMan 的字符串并将其写入请求流
或从 NuGet 安装免费的 RestSharp