当GetResponse()时,HttpWebRequest返回错误500

时间:2016-12-20 13:10:28

标签: c# .net xml soap httpwebrequest

我试图从SOAP webservice获得响应,但是我陷入了一种感觉错误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;
}
谁能帮帮我?我已经阅读了一系列文章,遗憾的是没有帮助我。非常感谢!

1 个答案:

答案 0 :(得分:0)

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