Created
November 18, 2013 17:23
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""",@""""); | |
ret = ret.Replace(">", @">"); | |
ret = ret.Replace("<", @"<"); | |
return ret; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment