到https服务器的HttpWebRequest在提琴手中起作用,但在Visual Studio中不起作用

|| 这是场景。我已经编写了代码,使用数字证书从安全的URL中获取cookie,然后使用检索到的cookie和相同的数字证书将数据回传到另一个URL。 GET工作和cookie被检索,POST返回并出现错误500。我安装了提琴手以查看发生了什么... POST看起来很好...存在cookie。我在fiddler中使用了该功能,该功能允许通过拖放创建请求。 POST与Fiddler中记录的C#代码中记录的POST完全相同,它可以工作! Fiddler在做什么而不是Visual Studio?如果提琴手可以过帐数据,但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();
    
已邀请:
如果您设置了代码,代码是否有效
req.ServicePoint.Expect100Continue = false; 
在您所有的WebRequest上?     
解决了!!!它与Expect100Continue无关。 因此,经过数周的故障排除.... 6个不同的程序员....我明白了。我不确定是否总是如此,但是在这种情况下,问题是我们从以下位置获取Cookie的网址:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(\"https://servertoGETcookies/fileUpload.html\");
与我们将数据发布回的网址不同:
req = (HttpWebRequest)WebRequest.Create(\"https://servertoPOSTdatawithcookies/main\");
获取cookie并发布回相同的URL可以解决此问题:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(\"https://servertoGETandPOSTcookies/main\");
req = (HttpWebRequest)WebRequest.Create(\"https://servertoGETandPOSTcookies/main\");
    

要回复问题请先登录注册