locked
Get following exception : Unhandled Exception: System.Net.WebException: The remote server returned an error: (415) Unsupported Media Type.

    Question

  • I get following error

    Unhandled Exception: System.Net.WebException: The remote server returned an erro
    r: (415) Unsupported Media Type.
       at System.Net.HttpWebRequest.GetResponse()

    This is my program. I am making POST to the freely available webservice http://www.webservicex.net/ConvertTemperature.asmx?op=ConvertTemp.    Please can you point me what is the error?

    namespace testwebservice
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Console.WriteLine("Calling web method ConvertTemp() using HTTP");
                string postdata = "Temperature=100&FromUnit=degreeCelsius&ToUnit=degreeFahrenheit";
                byte[] buffer = Encoding.ASCII.GetBytes(postdata);
                string url = "http://www.webservicex.net/ConvertTemperature.asmx?op=ConvertTemp";
                
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = buffer.Length;
                req.Headers.Add("SOAPAction:\"http://www.webserviceX.NET/ConvertTemp\"");
                req.Timeout = 5000;
                Stream reqst = req.GetRequestStream();
                reqst.Write(buffer, 0, buffer.Length);
                reqst.Flush();
                reqst.Close();
                
    
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                Stream resst = res.GetResponseStream();
                StreamReader sr = new StreamReader(resst);
    
                Console.WriteLine("Http response is: ");
                Console.WriteLine(sr.ReadToEnd());
                sr.Close();
                resst.Close();
                
                Console.WriteLine("done");
            }
        }
    }
    


    Sunday, February 19, 2012 11:55 PM

All replies

  • Why don't you just add a service reference to the service, and call it that way? Why play with HttpWebRequest?

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    Monday, February 20, 2012 2:54 AM
    Moderator
  • Please can you send me link to the article that you are mentioning so i can understand how to use it?
    Monday, February 20, 2012 3:07 AM
  • I wasn't mentioning an article. If you need one, try "How to Consume a Web Service".

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    Monday, February 20, 2012 3:11 AM
    Moderator