【发布时间】:2023-09-21 08:44:02
【问题描述】:
这里是场景。我编写了代码,使用数字证书从安全 url 获取 cookie,然后使用检索到的 cookie 和相同的数字证书将数据 POST 回另一个 url。 GET 工作并检索 cookie,POST 返回错误 500。我安装了 fiddler 以查看发生了什么...POST 看起来很好...cookies 存在。我在 fiddler 中使用了允许通过拖放创建请求的功能。发布与 Fiddler 中记录的 C# 代码中记录的完全相同的 POST,它可以正常工作!
Fiddler 做了哪些 Visual Studio 没有做的事情?如果 fiddler 可以 POST 数据但 Visual Studio 返回错误 500,它一定是在做某事。下面是代码:
X509Certificate cert = new X509Certificate("mycert.pfx", "certpassword");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://servertoGETcookies/fileUpload.html");
req.CookieContainer = new CookieContainer();
req.Method = "GET";
req.ClientCertificates.Add(cert);
HttpWebResponse Response = (HttpWebResponse)req.GetResponse();
CookieCollection ck = req.CookieContainer.GetCookies(req.RequestUri);
string strcookie = ck[0].Value;
string strcookie2 = ck[1].Value;
Response.Close();
req = (HttpWebRequest)WebRequest.Create("https://servertoPOSTdatawithcookies/main");
req.CookieContainer = new CookieContainer();
Cookie auth = new Cookie("_wl_authcookie_", strcookie2);
Cookie jsess = new Cookie("JSESSIONID", strcookie);
auth.Domain = "server";
jsess.Domain = "server";
req.CookieContainer.Add(auth);
req.CookieContainer.Add(jsess);
req.ClientCertificates.Add(cert);
req.Method = "POST";
Byte[] data = ReadByteArrayFromFile("filewithdatatoPOST.txt");
req.ContentLength = data.Length;
Stream myStream = req.GetRequestStream();
myStream.Write(data, 0, data.Length);
myStream.Close();
HttpWebResponse Response2 = (HttpWebResponse)req.GetResponse();
Stream strm = Response2.GetResponseStream();
StreamReader sr2 = new StreamReader(strm);
Response2.Close();
【问题讨论】:
标签: c# post httpwebrequest fiddler