【问题标题】:HttpWebRequest return error 500 when GetResponse()GetResponse() 时 HttpWebRequest 返回错误 500
【发布时间】:2016-12-20 13:10:27
【问题描述】:

我正在尝试从 SOAP 网络服务获取响应,但我遇到了网络异常错误 500。

这是我的班级得到回应:

public static string getResponse()
{
    StringBuilder xml = new StringBuilder();
    //xml.Append(@"<?xml version=""1.0"" encoding=""utf-8""?>");
    xml.Append(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:br=""http://www.totvs.com.br/br/"" >").Replace(@"\", "");
    xml.Append("<soap:Envelope/>");
    xml.Append("<soapenv:Body>");
    xml.Append("<br:RealizarConsultaSQLAuth>");
    xml.Append("<br:codSentenca>GLOBAL_054</br:codSentenca>");
    xml.Append("<br:codColigada>0</br:codColigada>");
    xml.Append("<br:codAplicacao>V</br:codAplicacao>");
    xml.Append("<br:Usuario>xxxx</br:Usuario>");
    xml.Append("<br:Senha>xxxx</br:Senha>");
    xml.Append("<br:parameters>codcoligada=1;codsistema=V;codusuario=mestre</br:parameters>");
    xml.Append("</br:RealizarConsultaSQLAuth>");
    xml.Append("</soapenv:Body>");
    xml.Append("</soapenv:Envelope>");
    string s = getUKMailData(xml.ToString(), "http://xxx.xxxx.com.br:99/xxxx/wsConsultaSQL.asmx");
    return s;
}

public static string getUKMailData(string xml, string address)
{
    string result = "";
    HttpWebRequest request = CreateWebRequest(address);
    XmlDocument soapEnvelopeXml = new XmlDocument();
    string teste = xml.Replace(@"\", "");
    soapEnvelopeXml.LoadXml(teste);
    try
    {
        using (Stream stream = request.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(response.GetResponseStream()))
            {
                string soapResult = rd.ReadToEnd();
                result = soapResult;
            }
        }
    }
    catch (WebException wex)
    {
        var pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
    }
    return result;
}

public static HttpWebRequest CreateWebRequest(string url)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Headers.Add("SOAP:Action");
    webRequest.Headers.Add("username", "xxx");
    webRequest.Headers.Add("password", "xxx");
    // webRequest.ContentType = "text/xml;charset=\"utf-8\"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    webRequest.PreAuthenticate = true;
    webRequest.Credentials = CredentialCache.DefaultCredentials;
    return webRequest;
}

有人可以帮助我吗?我已经阅读了一系列文章,不幸的是这些文章对我没有帮助。非常感谢!

【问题讨论】:

  • 错误 500 表示服务器以某种方式处理您的请求时遇到错误。设计良好的 API 将通过进一步的代码和响应消息指示发生了什么。例如500.13 是网络服务器正忙。查看响应的内容,它可能包含诸如“'Usuario' 的值无效”之类的消息。我们不太可能仅通过阅读这段(我可能会添加非英语)代码来猜出问题所在。

标签: c# .net xml soap httpwebrequest


【解决方案1】:

您的请求可能是真的,但服务器端不知道如何处理您的数据。可能是发送错误的参数。请检查你的服务器端so端点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2017-12-29
    • 1970-01-01
    • 2018-06-01
    • 2011-05-05
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多