Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access an ASMX web service using a WebClient?

I am trying to create a method in a class that accesses a WebMethod on a Web Service using WebClient, and am running into problems. Using VS2010 on Windows.

First of all, yes I know I could create a Web Reference to the web service in the class library, of course this is design time binding. However, I need to be able to get to the web service using information only available at run time. There's a business reason for this that I won't go into here, just go with it, please.

This appears to be possible using the WebClient class from the System.Net namespace. And in fact I am able to get to the service in question, but the data I am sending to it doesn't appear to be in a correct format, although for all I can tell it is a properly formatted SOAP message.

The WebException contains the following message: "The remote server returned an error: (415) Unsupported Media Type."

Here's the code:

public string DoingBusiness()
{
    WebClient client = new WebClient();
    string destUri = "http://localhost/Service/Service.asmx?op=CommunicationsCheck";

    StreamReader reader = new StreamReader(@"CommCheck.xml");
    string data = String.Format(reader.ReadToEnd(), "The End is Near!");
    reader.Close();

    string response = client.UploadString(destUri, data);

    return response;
}

Leaving off the actual xmlns, which is sensitive, the data read by the StreamReader above looks like:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CommunicationsCheck xmlns="http://.../">
      <communication>{0}</communication>
    </CommunicationsCheck>
  </soap:Body>
</soap:Envelope> 

This looks like a perfectly fine SOAP message. Of course the "{0}" gets filled in with the payload string. If the WebMethod "CommunicationsCheck(string communication) ran successfully it would return:

<?xml version="1.0" encoding="utf-8" ?> 
<string xmlns="http://[service url]/">You sent 'The End is Near!'</string> 

Which it does if I access the service through a browser, or through a design-time web reference.

So, the question is, what am I doing wrong here that I get the "(415) Unsupported Media Type"

Are there properties on the WebClient that need to be set? Do I perhaps need to provide a Header containing a UserAgent? Or something else?

like image 498
Cyberherbalist Avatar asked Feb 20 '23 14:02

Cyberherbalist


1 Answers

Darn. I guess I should let someone else answer the question, but answering one's own question is acceptable, if one finds it oneself.

Anyway, some further research on the problem seemed to suggest that one surmise on my part, namely that there might be some Header property I needed to set, was a possible solution. And the research suggested that the property would be "content-type" needed to be set as "text/xml". So I added this line:

client.Headers.Add("content-type", "text/xml");

just before the UploadString method call.

BINGO!

The expected response occurred.

Has anyone ever noticed that answers to perplexing questions sometimes become self-evident once the question is posed, but not before? Interesting phenomenon.

like image 188
Cyberherbalist Avatar answered Feb 23 '23 12:02

Cyberherbalist