how to consume web service from desktop c# app?

246 views Asked by At

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?

2

There are 2 answers

1
John Saunders On BEST ANSWER

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

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

0
Lukasz On

I finally got this to work:

        var netCred = new NetworkCredential { UserName = "user1", Password = @"pass1" };
        WebClient proxy = new WebClient();
        proxy.Credentials = netCred;

        //Method 1 - Newtonsoft.Json


            string serviceURL =
               string.Format("https://sourcesite.com.com/api/external/v1/Snapshot-Accrual?isConfirmed="
                + Confirmed.ToString() + "&sinceDate=" + String.Format("{0:yyyy-MM-dd}", SinceDate);
            data = proxy.DownloadData(serviceURL);
            jsonString = Encoding.ASCII.GetString(data);

            ac1 = Newtonsoft.Json.JsonConvert.DeserializeObject<SnapshotAccruals>(jsonString);

Now if I only knew how to prompt for credentials instead of hard-coding them, I'd be set. I know that if I paste the serviceURL directly in the browser, it will ask me for credentials. If I omit them in the code I get (401) Unauthorized.