Skip to content

Instantly share code, notes, and snippets.

@walbon
Created November 18, 2013 17:23
Show Gist options
  • Save walbon/7531713 to your computer and use it in GitHub Desktop.
Save walbon/7531713 to your computer and use it in GitHub Desktop.
Workaround pra funcionamento de uma chamada SOAP que tem no response um cabeçalho diferente do que é definido no WSDL do serviço. Isso geralmente ocasiona o erro de mensagem NULA invés de erro. Para isso faço uma chamada via webrequest passando o XML como o BODY da mensagem, é claro tendo que encapsular dentro do ENVELOPE da mesma forma que a ap…
private static string WebRequestPostData(string url, string postData)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
req.ContentType = "text/xml";
req.Method = "POST";
string postDataBody = @"<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><soap:Body><xml><![CDATA[";
postDataBody += postData;
postDataBody += "]]></xml></soap:Body></soap:Envelope>";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postDataBody);
req.ContentLength = bytes.Length;
try
{
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
}
catch
{
return null;
}
using (System.Net.WebResponse resp = req.GetResponse())
{
if (resp == null) return null;
using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()))
{
string ret = sr.ReadToEnd().Trim();
ret = ret.Replace("\n", "");
ret = ret.Trim();
ret = ret.Replace("&quot;",@"""");
ret = ret.Replace("&gt;", @">");
ret = ret.Replace("&lt;", @"<");
return ret;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment