如何使用Https协议使用Web服务(由C#创建)

时间:2011-03-18 16:09:33

标签: c# web-services https security

我正在开发一个小项目,这是一个C#Web服务,我这样做但现在我想使用HTTPS协议运行Web服务,因为我已经在我的系统和IIS 5.1中安装了Web身份验证证书服务器在HTTPS协议下运行(我已在该目录安全性中配置)

但现在我想使用HTTPS协议调用Web服务,有人告诉我,我需要修改该Web服务的WSDL文件,但我不知道该怎么做......

现在我的服务网址就像这样.... http://localhost:2335/SWebService.asmx

这里我想使用https而不是http

1 个答案:

答案 0 :(得分:1)

实例化Web服务代理类时,可以使用Url参数覆盖Web服务的URL。

如果您获得了所需的网址,则可以在此处进行设置。

建议从配置文件中获取所需的URL,并设置一个服务于Web服务代理的工厂类。

MyWebService clientProxy = new MyWebService();
clientProxy.Url = "https://localhost:2335/SWebService.asmx";

// or better still
// clientProxy.Url = ConfigurationSettings.AppSettings["webServiceUrl"];

这种方法对于上线非常有用,因为您需要一个实时的Web服务端点。

在此处添加工厂类:

public static class WebServiceFactory
{
    public static MyWebService GetMyWebService()
    {
        MyWebService clientProxy = new MyWebService();
        clientProxy.Url = ConfigurationSettings.AppSettings["webServiceUrl"];
        return clientProxy;
    }
}

意味着您可以像这样获得客户端代理:

MyWebService clientProxy = WebServiceFactory.GetMyWebService();
string exampleText = clientProxy.GetExampleText();

以下是web.config文件的示例:

<configuration>
<appSettings> 
        <add key="webServiceUrl" value="https://localhost:2335/SWebService.asmx" />
</appSettings>
相关问题