使用 C# Service Reference 与代理

7
我正在尝试在C#项目中使用ServiceReference。该项目旨在测试连接。我的客户正在尝试使用C#应用程序连接到我的同事的一个Web服务。连接无法建立,他认为这是我们自己的错误。
我现在正在尝试编写一个简单的C#项目。 故事讲到这里就够了...现在需要的信息如下:
  1. 客户使用代理服务器
  2. 我尝试连接到此Web服务进行测试 http://www.w3schools.com/webservices/tempconvert.asmx
  3. 我使用 .Net Framework 4.0
  4. 我的项目编译成Windows窗体应用程序。
这里是我的方法源代码:
    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            //Create Client
            ServiceReference1.TempConvertSoapClient client = new ServiceReference1.TempConvertSoapClient(@"TempConvertSoap",@"http://www.w3schools.com/webservices/tempconvert.asmx");
            if (client.ClientCredentials != null)
            {
                //Use Values which are typed in in the GUI
                string user = tbUser.Text;
                string password = tbPassword.Text;
                string domain = tbDomain.Text;

                //Check what information is used by the customer.
                if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(domain))
                {
                    client.ClientCredentials.Windows.ClientCredential = new NetworkCredential(user, password, domain);
                }
                if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(password))
                {
                    client.ClientCredentials.Windows.ClientCredential = new NetworkCredential(user, password);
                }
            }
            //Oh nooo... no temperature typed in
            if (string.IsNullOrEmpty(tbFahrenheit.Text))
            {
                //GOOD BYE
                return;
            }
            //Use the webservice 
            string celsius =  client.FahrenheitToCelsius(tbFahrenheit.Text); //<-- Simple Calculation
            tbCelsius.Text = celsius;
        }
        catch(Exception ex) 
        {
            //Something 
            MessageBox.Show(ex.ToString());
        }

    }

这是我的问题: 如何为此服务引用或客户端设置代理? 没有针对此目的的属性或setter。我尝试使用ClientCredentials。

2个回答

13

好的,我自己找到了答案。希望能帮助到别人;)。

这部分创建了一个绑定。稍后可以在 Web 服务中使用它。

    private void button2_Click(object sender, EventArgs e)
    {
        BasicHttpBinding binding = new BasicHttpBinding("TempConvertSoap");

        if (!string.IsNullOrEmpty(tbProxy.Text))
        {
            binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;


            string proxy = string.Format("http://{0}", tbProxy.Text);
            if (!string.IsNullOrEmpty(tbPort.Text)) 
            {
               proxy = string.Format("{0}:{1}",proxy,tbPort.Text);
            }

            binding.UseDefaultWebProxy = false;
            binding.ProxyAddress = new Uri(proxy);


        }
        EndpointAddress endpoint = new EndpointAddress(@"http://www.w3schools.com/webservices/tempconvert.asmx");

这里开始是旧部分,我在这里设置了绑定。

            try
        {
            //Create Client
            ServiceReference1.TempConvertSoapClient client = new ServiceReference1.TempConvertSoapClient(binding, endpoint);
            if (client.ClientCredentials != null)
            {
                //Use Values which are typed in in the GUI
                string user = tbUser.Text;
                string password = tbPassword.Text;
                string domain = tbDomain.Text;



                client.ClientCredentials.UserName.UserName = user;
                client.ClientCredentials.UserName.Password = password;
                client.ClientCredentials.Windows.ClientCredential.Domain = domain;

                //Check what information is used by the customer.
                if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(domain))
                {
                    client.ClientCredentials.Windows.ClientCredential = new NetworkCredential(user, password, domain);
                }
                if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(password))
                {
                    client.ClientCredentials.Windows.ClientCredential = new NetworkCredential(user, password);
                }
            }
            //Oh nooo... no temperature typed in
            if (string.IsNullOrEmpty(tbFahrenheit.Text))
            {
                //GOOD BYE
                return;
            }
            //Use the webservice 

            //THIS ONE IS IMPORTANT
            System.Net.ServicePointManager.Expect100Continue = false;
            string celsius =  client.FahrenheitToCelsius(tbFahrenheit.Text); //<-- Simple Calculation
            tbCelsius.Text = celsius;
        }
        catch(Exception ex) 
        {
            //Something 
            tbCelsius.Text = ex.Message;
            MessageBox.Show(ex.ToString());
        }

    }

我使用Squid作为我的代理服务器,并在代理服务器之外使用了防火墙。 在我成功配置后,我遇到了错误(417)Expectation failed。 在研究期间,我找到了一行代码,帮助我“解决”了这个问题。

System.Net.ServicePointManager.Expect100Continue = false;

6

虽然这是一个老问题,但仍然很重要。接受的答案可以解决问题,但我用了一种不同的方法来解决它。当你在项目中添加服务引用时,Visual Studio会将绑定和终结点地址等内容添加到web.config/app.config文件中(取决于应用程序类型)。你可以直接将代理配置(ip和端口)添加到配置文件中,这样就不需要在代码端做任何特殊处理:

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>        
        <binding name="MyServiceBinding" 
            proxyAddress="http://123.123.12.1:80" useDefaultWebProxy="false" />
      </basicHttpBinding>
    </bindings>
    <client>    
      <endpoint address="http://address.com/endpoint"
        binding="basicHttpBinding" bindingConfiguration="MyServiceBinding"
        contract="..." name="..." />
    </client>
  </system.serviceModel>

所以将IP和端口更改为您的代理服务器IP和端口,并记得使用useDefaultWebProxy =“false”,这样应用程序将使用此服务引用的代理。

basicHttpBinding参考。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接