Search code examples
web-servicesxml-serializationdatacontractserializer

how to consume web service from desktop c# app?


I'm trying to consume a HTTP web service returning XML generated by a DataContractSerializer in a C# Desktop application. The link to the service doesn't have any aspx or svc extention, and the parameters are expected in the link.

https://sourcesite.com/api/external/v1/Snapshot?isConfirmed={isConfirmed}&sinceDate={sinceDate}&pageNumber={pageNumber}&recordsPerPage={recordsPerPage}

It works when I use it in a browser (after providing proper parameter values). When run for the first time it asked me my credentials which it accepted and didn't ask again, even after the browser restart.

I tried adding a web reference (using VS2013), using url without the parameters but I'm getting errors:

The remote server returned an unexpected response: (405) Method Not Allowed.

If I add hardcoded parameters, I get:

The document at the url https://sourcesite.com/api/external/v1/Snapshot?isConfirmed=True was not recognized as a known document type. The error message from each known type may help you fix the problem: - Report from 'XML Schema' is 'Data at the root level is invalid. Line 1, position 1.'. - Report from 'DISCO Document' is 'Data at the root level is invalid. Line 1, position 1.'. - Report from 'WSDL Document' is 'There is an error in XML document (1, 1).'. - Data at the root level is invalid. Line 1, position 1. Metadata contains a reference that cannot be resolved: 'https://sourcesite.com/api/external/v1/Snapshot?isConfirmed=True'. The remote server returned an unexpected response: (405) Method Not Allowed. The remote server returned an error: (405) Method Not Allowed. If the service is defined in the current solution, try building the solution and adding the service reference again.

Is the service incorrectly configured, or am I missing something? Perhaps there's a different way to connect to this service form the code?


Solution

  • "Add Service Reference" and "Add Web Reference" are for SOAP web services.

    You'll have to use the WebClient class and "do it yourself".