获取以下异常:未处理的异常:System.Net.WebException:远程服务器返回错误:(415)不支持的媒体类型。 [英] Get following exception : Unhandled Exception: System.Net.WebException: The remote server returned an error: (415) Unsupported Media Type.

查看:847
本文介绍了获取以下异常:未处理的异常:System.Net.WebException:远程服务器返回错误:(415)不支持的媒体类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误

未处理的异常:System.Net.WebException:远程服务器返回了错误信号

r:(415)不支持的媒体输入。

  在System.Net.HttpWebRequest.GetResponse()

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

这是我的程序。我正在POST免费提供的网络服务
http://www.webservicex.net/ConvertTemperature.asmx ?OP = ConvertTemp
功能。  请问你能指出我的错误是什么吗?

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");
        }
    }
}

推荐答案

为什么不向服务添加服务引用,并以这种方式调用它?为什么要使用HttpWebRequest?
Why don't you just add a service reference to the service, and call it that way? Why play with HttpWebRequest?


这篇关于获取以下异常:未处理的异常:System.Net.WebException:远程服务器返回错误:(415)不支持的媒体类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆