简体   繁体   English

Webrequest通过HTTP而不是通过HTTPS(SSL)工作

[英]Webrequest work over HTTP but not over HTTPS (SSL)

We have a rest service which works fine over http. 我们有一个可以在http上正常运行的休息服务。 I use the following C# code to post data to it: 我使用以下C#代码向其发布数据:

    string postData = [contains the data to post]
    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    var baseAddress = "http://www.myserver.net/rest/saveData";
    System.Net.WebRequest req = System.Net.WebRequest.Create(baseAddress);
    string authInfo = "username:password";
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    req.Headers["Authorization"] = "Basic " + authInfo;
    req.ContentType = "application/x-www-form-urlencoded";
    req.Method = "POST";

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData);
    req.ContentLength = bytes.Length;

    System.IO.Stream os = req.GetRequestStream();
    os.Write(bytes, 0, bytes.Length); //Push it out there
    os.Close();

    System.Net.WebResponse resp = req.GetResponse();
    var s = resp.GetResponseStream();

    var sr = new System.IO.StreamReader(s);
    var r= sr.ReadToEnd().Trim();

Now if change this to SSL, replacing the http call with a https call: 现在,如果将其更改为SSL,则将HTTP调用替换为https调用:

"https://www.myserver.net/rest/saveData";

This does not work, as it timeout at the GetReponse call. 这不起作用,因为它在GetReponse调用中超时。 First I thought is was caused by a self signed certificate, but this is not the case as I use: 首先,我认为这是由自签名证书引起的,但事实并非如此:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

Which accepts all certificates. 接受所有证书。 The error I get is something like zero bytes returned on transportlayer (it is in my language, so I don't know exactly what the english exception is) 我得到的错误类似于在传输层上返回的零字节(这是我的语言,所以我不确切知道英语异常是什么)

I use CURL on the commandline to test the calls and there it works both on http and SSL, so the rest service seems to fine. 我在命令行上使用CURL来测试呼叫,并且该代码在HTTP和SSL上都可以使用,因此其余服务似乎正常。

Does anybody have an idea why it does not work over SSL? 有人知道为什么它不能通过SSL工作吗?

Thanks 谢谢

I found out the reason for the problem. 我找出了问题的原因。 The rest service is talking SSL3 in stead of TLS. 其余服务使用SSL3代替TLS。 By adding the following code: 通过添加以下代码:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

The problem is solved. 问题已经解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM