如何在带有 SSL 的 C# 中使用 HTTP GET 请求?(违反协议)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/708210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use HTTP GET request in C# with SSL? (protocol violation)
提问by
I am currently trying to get a response from a server that is using SSL in C#. I have the code to do this in Java, but it looks like they do not translate 1:1.
我目前正在尝试从在 C# 中使用 SSL 的服务器获取响应。我有在 Java 中执行此操作的代码,但看起来它们不是 1:1 翻译。
I do have some code that I found that works for regular pages, but not for the one I need (maybe because of the SSL thing). Here is the code:
我确实有一些代码,我发现它们适用于常规页面,但不适用于我需要的页面(可能是因为 SSL 的原因)。这是代码:
WebRequest request = WebRequest.Create("https://" + sslServerHost + ":" + sslServerPort);
request.Proxy = null;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
UPDATE: Sorry I seem to have forgotten what the error is. I'm getting a protocol violation exception at the "HttpWebResponse response = (HttpWebResponse)request.GetResponse(); " line. Any ideas? Thanks guys.
更新:抱歉,我似乎忘记了错误是什么。我在“HttpWebResponse response = (HttpWebResponse)request.GetResponse();”行收到协议违规异常。有任何想法吗?谢谢你们。
回答by Marc Gravell
Just an idea, but have you tried it with a final "/"? Also - you might find this approach easier:
只是一个想法,但您是否尝试过使用最后的“/”?此外 - 您可能会发现这种方法更容易:
string s;
using(WebClient client = new WebClient()) {
client.UseDefaultCredentials = true;
s = client.DownloadString("https://" + sslServerHost + ":"
+ sslServerPort + "/");
}
回答by amit-agrawal
HTTP conversations over SSL use a properly issued certificate for validation.
通过 SSL 的 HTTP 对话使用正确颁发的证书进行验证。
You could use the RemoteCertificateValidationCallback delegate for validating the SSL certificate as follows:
您可以使用 RemoteCertificateValidationCallback 委托来验证 SSL 证书,如下所示:
public static void ConnectSSL()
{
WebRequest request = WebRequest.Create("https://" + sslServerHost + ":" + sslServerPort);
request.Proxy = null;
request.Credentials = CredentialCache.DefaultCredentials;
//allows for validation of SSL certificates
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
}
//for testing purpose only, accept any dodgy certificate...
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
回答by Dscoduc
Here is some test code that I have used successfully for testing SSL connections...
这是我成功用于测试 SSL 连接的一些测试代码...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.dscoduc.com");
//request.Method = "HEAD";
//request.AllowAutoRedirect = false;
request.Credentials = CredentialCache.DefaultCredentials;
// Ignore Certificate validation failures (aka untrusted certificate + certificate chains)
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
string responseFromServer = reader.ReadToEnd();