I am trying to consume a webservice that requires a username and password to connect. I have a working example in Java of how to do this, but I am trying to write the equivalent solution in C#.
Here is the Java code:
Code:
import myplace.MY3004527.webservice.MY3004527WebServiceBindingStub;
import myplace.MY3004527.webservice.MY3004527WebServiceServiceLocator;
public class MyManager {
MY3004527WebServiceBindingStub myWS;
public MyManager() { super(); }
public void initialize() throws ServiceException {
MY3004527WebServiceServiceLocator service = new MY3004527WebServiceServiceLocator();
myWS = new MY3004527WebServiceBindingStub(
new URL("https://myservice.someplace.com/MY3004527/MY3004527WebService"),
service);
myWS.setUsername("theuser");
myWS.setPassword("secretpassword");
}
.
.
.
I added a web reference in my Visual Studio C# Console application by entering the address to the wsdl. I had to enter the username and password for it to be able to see it and complete generation of the web service code.
(Note I have used SoapUI to verify the webservice is working correctly and after entering username password there it worked fine. Also, works with Java code.)
Here's what I have so far in my non-working C# code version:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using MyManagerConsumer.MyManagerWS;
using System.ServiceModel;
namespace MyManagerConsumer
{
class Program
{
MyManagerWS.MY3004527WebService myWS;
public void initialize()
{
//System.ServiceModel.Channels.Binding binding = new System.ServiceModel.Channels.Binding();
//ADPX1001635WebService service = new IOManagerWS.ADPX1001635WebServiceClient(binding, remoteAddress);
//System.ServiceModel.Channels.Binding binding = new System.ServiceModel.Channels.Binding
System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.Transport);
System.ServiceModel.EndpointAddress remoteAddress = new System.ServiceModel.EndpointAddress("https://myservice.someplace.com/MY3004527/MY3004527WebService");
//System.ServiceModel.Description.ClientCredentials loginCredentials= new System.ServiceModel.Description.ClientCredentials();
//loginCredentials.UserName.UserName = "theuser";
//loginCredentials.UserName.Password = "secretpassword";
System.ServiceModel.Description.ContractDescription contract = new System.ServiceModel.Description.ContractDescription("contractName");
System.ServiceModel.ChannelFactory factory = contract.Behaviors.Find<ChannelFactory>();
factory.Credentials.UserName.UserName = "theuser";
factory.Credentials.UserName.Password = "secretpassword";
System.ServiceModel.Description.ServiceEndpoint endpoint = new System.ServiceModel.Description.ServiceEndpoint(contract, binding, remoteAddress);
endpoint.Address = remoteAddress;
//var defaultCredentials =
//remoteAddress.
myWS = new MyManagerWS.MY3004527WebServiceClient(binding,remoteAddress);
}
.
.
.
Not sure how to get the endpoint into the call to create the MY3004527WebServiceClient it only takes a binding and endpoint address or a configuration name and endpoint address. Not sure how to use the configuration name.
I wonder if I can just modify my app.config to contain the user name and password?
Here's my current app.config contents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MY3004527WebServiceBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://10.104.102.15:8080/MY3004527/MY3004527WebService"
binding="basicHttpBinding" bindingConfiguration="MY3004527WebServiceBinding"
contract="MyManagerWS.MY3004527WebService" name="MY3004527WebServicePort" />
</client>
</system.serviceModel>
</configuration>
Right now when I try to connect to the webservice from C# I always get the following error message...
{"Could not connect to http://10.104.102.15:8080/MY3004527/MY3004527WebService. TCP error code 10061: No connection could be made because the target machine actively refused it 10.104.102.15:8080. "}
I assume it is because I haven't specified the user and password info correctly.
This seems like it shouldn't be that complicated and is driving me crazy.
Please let me know if you know how to do this in C#.
Thanks.
Bookmarks