Using C # service help with proxy

I am trying to use ServiceReference in a C # project. The project is designed to test the connection. I have a client who is trying to connect to a C # application to one web service of one of my colleagues. The connection cannot be made, and he assumes that this is our own mistake.

I am trying to write a simple C # project. This is enough history ... now I need information.

Here is the source code of my method:

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

Here is my question: How can I set the proxy server for this service link or, rather, for the client? There is no property or setter for this purpose. I tried it using ClientCredentials

+4
source share
1 answer

Ok, I found the answer myself. Hope this helps someone;).

This part creates a binding. It can be later used in webservice.

  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"); 

This is where the old part begins, where I set the binding.

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

I used Squid as a proxy server and used a firewall in addition to the proxy server. After I configured it successfully, I encountered an error (417). Waiting failed. During the research, I found a line of code that helped me "solve" this problem.

 System.Net.ServicePointManager.Expect100Continue = false; 
+6
source

Source: https://habr.com/ru/post/1494257/


All Articles