views:

101

answers:

2

For my application, I'm currently using a webservice to retrieve some information I need in XML format. The way I'm doing it now is via a HTTP web request, so the code looks something like this:

serviceURL = "http://longurl/webservice.ashx?apikey=key&action=getDetail&id=ID";

HttpWReq = (HttpWebRequest)WebRequest.Create(serviceURL);
HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();

dataXML.Load(HttpWResp.GetResponseStream());

I feel that including the entire URL inside my code looks really clunky, so I've thought of adding the web service as a web reference. However, I came across this error when trying to add the web reference:

The document at the url longurl/webservice.ashx was not recognized as a known document type.
The error message from each known type may help you fix the problem:
- Report from 'DISCO Document' is 'Discovery document at the URL longurl/webservice.ashx could not be found.'. - The document format is not recognized.
- Report from 'WSDL Document' is 'There is an error in XML document (2, 2).'. - was not expected.
- Report from 'XML Schema' is 'The root element of a W3C XML Schema should be and its namespace should be 'hTtp://www.w3.org/2001/XMLSchema'.'.

Am I missing any steps that I had to do before attempting to add the web service? Or is this a problem with the web service itself?

I'd also appreciate any advice on alternative methods to code this a bit more elegantly. Thanks.

+1  A: 

I would honestly just put that url in an AppSetting.

so that serviceURL = ConfigurationManager.AppSettings["ServiceUrl"];

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx

Min
Ah, cool. This seems like a good way to move the clunky stuff over. I'd probably have to take a look into encrypting the sensitive data stored inside though.Thanks.
Kronon
Well... I think they already thought of that. You can encrypt configuration sections in the application.http://msdn.microsoft.com/en-us/library/ms998280.aspxMind you that with Reflector any string you put in code is pretty much available to anyone.
Min
A: 

A "web reference" is intended for an "old-style" ASMX web service, a "service reference" for a WCF service. You can't use (or abuse) that functionality for what you're trying to do.

Either just put your URL into config as "Min" suggested, or then change to a real WCF service. With the WCF Rest Starter Kit, you can create these types of "HTTP Plain Old XML (POX)" services quite easily - see the Pluralsight screencast on that topic for more information.

Marc

marc_s
Thanks for the feedback. I'll definitely take a closer look at that tutorial video you've linked to and see what it has to offer.
Kronon
I wonder why you put my name in quotes...
Min
@Min: because Min as such could be misinterpreted as "minimum" - putting it in quotes should make it clear that it's a name of someone else who answered the question.
marc_s