Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7889577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:08:06+00:00 2026-06-03T06:08:06+00:00

I am trying to create a method in a class that accesses a WebMethod

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:08:07+00:00Added an answer on June 3, 2026 at 6:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a method that take 2 int array as the input
I'm trying to create a method that will sum two timeO objects and return
I am trying to create a method that accepts multiple types of controls -
I'm trying to create a method that will allow me to set properties within
I am trying to create a generic method that will read an attribute on
I am trying to create a clear method that would clear the array I
I'm trying to create a class that extends input stream Clojure via gen-class .
I'm trying to create a class that will contain functions for serializing/deserializing objects to/from
I've been trying to create a simple class that implements the ViewSwitcher.ViewFactory interface basing
I'm trying to create a class that inherits from ListViewItemCollection, and I got this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.