Shaddix
Shaddix

Reputation: 6109

How to consume web service via GET methods?

I have a normal 3rd party SOAP service with WSDL and stuff. The problem is - it only accepts GET requests. How can I access it in c#?

When I add that service to VS via Add Service Reference and try to use it as usual:

var service = new BaseSvcClient(
                 new BasicHttpContextBinding(), 
                 new EndpointAddress("http://some.internal.ip/WebServices/Base.svc"));
var ver = service.Version();

I see (via fiddler) that it actually sends POST requests and web-service responds with Endpoint not found error message. If I simply hit http://some.internal.ip/WebServices/Base.svc/Version in a browser the proper xml is returned.

I can use WebClient, but then I have to construct all the GET requests manually, which doesn't look good. Are there other solutions?

Upvotes: 0

Views: 422

Answers (2)

Shaddix
Shaddix

Reputation: 6109

I have found an answer that helped me a lot. Basically if I take an autogenerated interface for the client, decorate methods with [WebGet] and use

var cf = new WebChannelFactory<IBaseSvc2>(new Uri("..."));
var service = cf.CreateChannel();
var result = service.Version();

it all works well. That's not a perfect solution, since changes won't be picked up automatically, so may be there are other solutions?

P.S. an interface for a web service is now like:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName = "BaseService.IBaseSvc")]
    public interface IBaseSvc2
    {
        [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IBaseSvc/Version", ReplyAction = "http://tempuri.org/IBaseSvc/VersionResponse")]
        [WebGet]
        VersionInformation Version();
    }

Upvotes: 1

HatSoft
HatSoft

Reputation: 11201

You can achieve it by adding the protocols in config file

<webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
</webServices>

Upvotes: 0

Related Questions