简体   繁体   English

HttpWebRequest.GetResponse提供内部服务器错误

[英]HttpWebRequest.GetResponse giving Internal Server Error

I want to make a request to a web service with Http-Post approach. 我想使用Http-Post方法向Web服务发出请求。 Here is my code in C#. 这是我在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);
            }
        }

Here is my web service method and used class: 这是我的Web服务方法和使用的类:

    [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;
        }
    };

And here is my configuration in config file for web service: 这是我在Web服务的配置文件中的配置:

<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>

In Main, when i debug WebResponse response = request.GetResponse(); 在Main中,当我调试WebResponse response = request.GetResponse(); line, i get Internal Server Error (500) with Protocol Error status. 行,我收到带有协议错误状态的内部服务器错误(500)。

Any ideas? 有任何想法吗? Thank you all. 谢谢你们。

Use this code along with Try and Catch method 将此代码与Try and Catch方法一起使用

try
{
  // Your Code 
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpWebRequest.GetResponse()引发不可陷阱的错误 - HttpWebRequest.GetResponse() throws untrappable error HttpWebRequest.GetResponse()返回404错误 - HttpWebRequest.GetResponse() returning 404 Error 获取错误“使用HttpWebRequest.GetResponse()进行屏幕抓取时,远程服务器返回错误:(403)禁止” - Getting Error “The remote server returned an error: (403) Forbidden” when screen scraping using HttpWebRequest.GetResponse() 服务器重定向到未知位置时的HttpWebRequest.GetResponse - HttpWebRequest.GetResponse when server redirects to unknown location HttpWebRequest.GetResponse() 抛出“错误请求”400 错误 - HttpWebRequest.GetResponse() throws “Bad Request” 400 error HttpWebRequest.GetResponse() 失败时如何获取错误信息 - How to get error information when HttpWebRequest.GetResponse() fails HttpWebRequest.getResponse() 返回 NULL - HttpWebRequest.getResponse() returning NULL Web服务中的httpwebrequest.getResponse超时 - httpwebrequest.getResponse timesout in webservice C#HttpWebRequest.GetResponse()返回错误:(404)找不到 - c# HttpWebRequest.GetResponse() returned an error: (404) Not Found HttpWebRequest.GetResponse方法卡住了特定的网址 - HttpWebRequest.GetResponse method stucks for specific urls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM