我的任务是从IBM服务器下载附件。 问题是,当我使用C#-HttpWebRequest和HttpWebResponse进行请求时,总是得到状态码500和响应正文“错误500:SRVE0295E:报告错误:500”。但是,当我使用“邮递员”执行相同的请求时,没有错误,并且文件已下载。 请注意以下事实,即需要一个代表当前会话的cookie。我向邮递员和我的请求添加了相同的cookie(可重复使用cookie)。
这是Postman的Http原始请求(我会发布一个屏幕截图,但缺乏信誉点):
https://eforms.ebase.dlh.de:443/forms/secure/org/run/service/ContentStorageService44084
获取/ forms / secure / org / run / service / ContentStorageService44084 HTTP / 1.1 主机:eforms.ebase.dlh.de:443 Cookie:IPCZQX03e54bbd1d = 090038003914048a6f27dcb2bc9b9e0c3a6c63b0;路径= /; domain = dlh.de
邮递员工作(这实际上是一个非常简单的GET请求,只有两个标头)。
这是C#代码(向我返回“错误500:SRVE0295E:报告错误:500”)
HttpWebRequest req = null;
HttpWebResponse res = null;
req= (HttpWebRequest)WebRequest.Create("https://eforms.ebase.dlh.de:443/forms/secure/org/run/service/ContentStorageService44084");
req.Headers.Add("Cookie", "IPCZQX03e54bbd1d=090038003914048a6f27dcb2bc9b9e0c3a6c63b0; path=/; domain=dlh.de");
req.Host = "eforms.ebase.dlh.de";
req.Method = "GET";
try
{
res = (HttpWebResponse)req.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse) ex.Response;
}
Stream receiveStream = res.GetResponseStream();
StreamReader sr = new StreamReader (receiveStream, Encoding.UTF8);
string responseBody = sr.ReadToEnd();
Console.WriteLine((int)res.StatusCode + "\t" + res.StatusCode.ToString());
Console.WriteLine(responseBody);
任何提示都会非常吸引人。
PS:cookie可以,没有有效的cookie,我什至不会收到“错误500:SRVE0295E:错误报告:500”。因此错误消息来自IBM服务器,我可以肯定地说。
谢谢。
约翰·科瓦连科
答案 0 :(得分:0)
好的,我找到了解决方案。我不得不假装自己是一个著名的浏览器。 因此,通过添加
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";
服务器将文件退还给我。