【问题标题】:HttpWebRequest.GetResponse giving Internal Server ErrorHttpWebRequest.GetResponse 给出内部服务器错误
【发布时间】:2014-11-22 14:09:15
【问题描述】:

我想使用 Http-Post 方法向 Web 服务发出请求。这是我的 C# 代码。

        static void Main(string[] args)
        {
            var _url = "http://localhost/EventWebService/EventWebService.asmx";
            var _action = "http://localhost/EventWebService/EventWebService.asmx?op=GetPerson";

            XmlDocument soapEnvelope = new XmlDocument();
            soapEnvelope.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><GetPerson xmlns=""http://tempuri.org/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><name>Brad</name><surname>Pitt</surname><age>51</age></GetPerson></SOAP-ENV:Body></SOAP-ENV:Envelope>");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
            request.Headers.Add("SOAPAction", _action);
            request.ContentType = "text/xml;charset=\"utf-8\"";
            request.Accept = "text/xml";
            request.Method = "POST";
            InsertSoapEnvelopeIntoWebRequest(soapEnvelope, request);
            WebResponse response = request.GetResponse();
            StreamReader rd = new StreamReader(response.GetResponseStream());
            Console.WriteLine(rd.ReadToEnd());
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }

这是我的网络服务方法和使用的类:

    [WebMethod]
    [ return: XmlElement("ReturnValueElement",IsNullable=false)]
    public Person GetPerson([XmlElement("Name")] string name, [XmlElement("Surname")] string surname, [XmlElement("Age")] int age)
    {
        return new Person(name, surname, age);
    }

    public class Person
    {
        string Name { get; set; }
        string Surname { get; set; }
        int Age { get; set; }

        public Person() { }

        public Person(string _name, string _surname, int _age)
        {
            Name = _name;
            Surname = _surname;
            Age = _age;
        }
    };

这是我在网络服务配置文件中的配置:

<configuration>
  <system.web>
    <webServices>
        <protocols>
            <add name="HttpSoap"/>              
            <add name="Documentation"/>
            <add name="HttpPostLocalhost"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
</configuration>

在 Main 中,当我调试 WebResponse response = request.GetResponse(); 行时,我收到 Internal Server Error (500) 和 Protocol Error 状态。

有什么想法吗?谢谢大家。

【问题讨论】:

  • 为什么不使用常规的网络参考或 WCF?

标签: c# web-services http-post internal-server-error protocolexception


【解决方案1】:

将此代码与 Try and Catch 方法一起使用

try
{
  // Your Code 
}

catch(Exception wex)
{
  string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
  return pageContent;
}

【讨论】:

    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多