使用C#服务引用与代理

Bon*_*ngo 5 c# proxy web-services service-reference

我试图在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());
        }

    }
Run Code Online (Sandbox Code Playgroud)

这是我的问题: 如何设置代理到此服务引用或客户端?没有用于此目的的财产或制定者.我尝试使用ClientCredentials

Bon*_*ngo 8

好吧,我自己找到了答案.希望它会帮助某人;).

这部分创建了一个绑定.这可以在以后用于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");
Run Code Online (Sandbox Code Playgroud)

这里开始我设置绑定的旧部分.

            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());
        }

    }
Run Code Online (Sandbox Code Playgroud)

我使用Squid作为我的代理,并使用除代理之外的防火墙.在我成功配置后,我遇到了错误(417)期望失败.在研究过程中,我发现了一行代码,帮助我"解决"了这个问题

System.Net.ServicePointManager.Expect100Continue = false;
Run Code Online (Sandbox Code Playgroud)


Esk*_*sko 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>
Run Code Online (Sandbox Code Playgroud)

因此,将 ip 和端口更改为您的代理服务器 ip 和端口,并记住使用 useDefaultWebProxy="false" 以便应用程序将使用该代理与此服务引用。

basicHttpBinding 的参考。