【发布时间】:2018-03-06 16:24:28
【问题描述】:
我正在尝试使用 .Net Core 2.0 应用程序模拟此操作,代码如下:
static void Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://epaper.20minuten.ch/index.cfm/epaper/1.0/getEditionDoc");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
var serializer = new Newtonsoft.Json.JsonSerializer();
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
using (var tw = new Newtonsoft.Json.JsonTextWriter(streamWriter))
{
serializer.Serialize(tw,
new
{
editions = new[]
{
new
{
defId = "648",
publicationDate = "2018-03-06"
}
},
isAttachment = true,
fileName = "Gesamtausgabe_Lausanne_2018-03-06.pdf"
});
}
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
Console.ReadLine();
}
}
但我收到 500 个错误。我错过了什么?
【问题讨论】:
-
您使用了很多在这里并不理想的方法。大多数时候你想使用 HttpClient 来发出 Restful 请求。另外,尝试使用
JsonConvert.SerializeObject(),它通常是我使用的以及大多数代码示例使用的。 -
isAttachment拼写错误。我会运行 Fiddler,并比较好请求和坏请求的负载 -
@maccettura 我愿意接受建议,你会怎么做?
-
@maccettura 我的错,我从我的评论中删除了。
-
如果您对使用 RestSharp 感兴趣,您可以让 Postman 通过单击“代码”链接并从下拉列表中选择
C# (RestSharp)为您生成该 c# 代码。
标签: c# json post httpwebrequest webrequest