以下是该方案。我编写的代码使用数字证书从安全URL获取cookie,然后使用检索到的cookie和相同的数字证书将数据POST回到另一个URL。 GET工作和cookie被检索,POST返回错误500.我安装了fiddler以查看发生了什么... POST看起来很好......存在cookie。我在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();
答案 0 :(得分:0)
如果设置
,您的代码是否有效req.ServicePoint.Expect100Continue = false;
在您的所有WebRequests上?
答案 1 :(得分:0)
解决!!!它与Expect100Continue无关。
经过数周的故障排除...... 6位不同的程序员......我想通了。我不确定这是否总是如此,但在这种情况下问题是我们从以下网址获取cookie:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://servertoGETcookies/fileUpload.html");
与我们将数据发回的网址不同:
req = (HttpWebRequest)WebRequest.Create("https://servertoPOSTdatawithcookies/main");
获取Cookie并回发到相同的网址可以解决问题:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://servertoGETandPOSTcookies/main");
req = (HttpWebRequest)WebRequest.Create("https://servertoGETandPOSTcookies/main");