winform访问https webservice

参考:https://www.bbsmax.com/A/x9J21Naez6/

 

先执行以下代码,所以密钥验证都返回成功,否则调用https时会提示认证失败。

 ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                // if (errors == SslPolicyErrors.None)
                //    return true;
                return true;
            };

 

 

添加web引用,再执行:

  localhost.WebService1 w = new localhost.WebService1();
  w.Url = this.textBox1.Text;    //这可以重写app.config中url地址,使用一个不一样的asmx
 this.textBox2.Text = w.HelloWorld();

 

避免用户在浏览器上打开asmx查询接口信息的方法:

开发时正常处理,发布时在web.config中的 <system.web>节点增加添加以下:

  <webServices>
        <protocols>
          <add name="HttpSoap" />
          <add name="HttpPost" />
          <add name="HttpGet" />
          <add name="Documentation" />
        </protocols>
        <wsdlHelpGenerator href ="webform1.aspx" /> 这个是一个错误页面,请写一个简单的页面
      </webServices>

经过以上处理,客户端程序调用不受影响,但通过浏览器打开asmx时会跳转到上述的错误页面

posted @ 2021-06-15 15:44  81  阅读(463)  评论(0编辑  收藏  举报