我知道你们都会回答"使用像Fiddler这样的调试代理服务器",但事情并非那么简单.
这是我的情况:我有一些代码在服务器上运行,在ASP.NET页面代码隐藏(aspx.cs)中,其中(除其他外)建立与另一个服务器的连接,抓取一些东西,然后格式化它并将其返回给浏览器.
问题是其他服务器做错了,所以我希望能够将调试标志传递到页面中(通过查询字符串,例如?debug = true),这样它就会打印出完全原始的 HTTP请求它发送到其他服务器所以我可以看到什么是错的.这段代码在几个地方运行,所以我希望能够在dev,staging或production上传递这个标志,只看到请求,而不必弄清楚生产服务器是否可以与某处存在的代理服务器通信等
你会认为这样做很容易,对吧?所以我觉得我疯了或者别的什么,但我查看了HttpWebRequest及其父类WebRequest的参考资料 - 什么都没有.没有办法.你会认为微软会想到这一点.最接近的是你可以访问"Headers"集合,但是当我尝试它时,它省略了一些非常重要的标题,如"内容长度" - 所以它必须"撒谎"给我(我知道它在撒谎,因为我知道因为远程服务器返回200状态 - 请求成功,它只返回错误/不同/错误的数据)
这是ask-for代码示例:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.whatever.com");
req.Method = ... whatever ...;
... other setup for the request ...
/* At this point we are about to send the request.
What does the raw HTTP request look like? */
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Run Code Online (Sandbox Code Playgroud) 我有一个p12证书,我以这种方式加载它:
X509Certificate2 certificate = new X509Certificate2(certName, password,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet |
X509KeyStorageFlags.Exportable);
Run Code Online (Sandbox Code Playgroud)
它是正确加载的,实际上如果我这样做certificate.PrivateKey.ToXmlString(true);
会返回一个完整的xml而不会出错.但如果我这样做:
try
{
X509Chain chain = new X509Chain();
var chainBuilt = chain.Build(certificate);
Console.WriteLine("Chain building status: "+ chainBuilt);
if (chainBuilt == false)
foreach (X509ChainStatus chainStatus in chain.ChainStatus)
Console.WriteLine("Chain error: "+ chainStatus.Status);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Run Code Online (Sandbox Code Playgroud)
它写道:
Chain building status: False
Chain error: RevocationStatusUnknown
Chain error: OfflineRevocation
Run Code Online (Sandbox Code Playgroud)
所以当我这样做时:
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
ServicePointManager.Expect100Continue = true;
Console.WriteLine("connessione a:" + …
Run Code Online (Sandbox Code Playgroud)