SOAP - HTTP REQUEST
Bom tarde galera,
Preciso trocar informações entre a minha aplicação c# com um Webservice porém preciso realizar isso utilizado de SOAP via Http Request e nunca realizei tal façanha.
Por favor alguém já trabalhou com isso?
Grato
Preciso trocar informações entre a minha aplicação c# com um Webservice porém preciso realizar isso utilizado de SOAP via Http Request e nunca realizei tal façanha.
Por favor alguém já trabalhou com isso?
Grato
using System.Xml;
using System.Net;
using System.IO;
public static void CallWebService()
{
var _url = [Ô]http://xxxxxxxxx/Service1.asmx[Ô];
var _action = [Ô]http://xxxxxxxx/Service1.asmx?op=HelloWorld[Ô];
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add([Ô]SOAPAction[Ô], action);
webRequest.ContentType = [Ô]text/xml;charset=\[Ô]utf-8\[Ô][Ô];
webRequest.Accept = [Ô]text/xml[Ô];
webRequest.Method = [Ô]POST[Ô];
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.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><HelloWorld xmlns=[Ô][Ô]http://tempuri.org/[Ô][Ô] SOAP-ENV:encodingStyle=[Ô][Ô]http://schemas.xmlsoap.org/soap/encoding/[Ô][Ô]><int1 xsi:type=[Ô][Ô]xsd:integer[Ô][Ô]>12</int1><int2 xsi:type=[Ô][Ô]xsd:integer[Ô][Ô]>32</int2></HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>[Ô]);
return soapEnvelop;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
using System.Net;
using System.IO;
public static void CallWebService()
{
var _url = [Ô]http://xxxxxxxxx/Service1.asmx[Ô];
var _action = [Ô]http://xxxxxxxx/Service1.asmx?op=HelloWorld[Ô];
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add([Ô]SOAPAction[Ô], action);
webRequest.ContentType = [Ô]text/xml;charset=\[Ô]utf-8\[Ô][Ô];
webRequest.Accept = [Ô]text/xml[Ô];
webRequest.Method = [Ô]POST[Ô];
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.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><HelloWorld xmlns=[Ô][Ô]http://tempuri.org/[Ô][Ô] SOAP-ENV:encodingStyle=[Ô][Ô]http://schemas.xmlsoap.org/soap/encoding/[Ô][Ô]><int1 xsi:type=[Ô][Ô]xsd:integer[Ô][Ô]>12</int1><int2 xsi:type=[Ô][Ô]xsd:integer[Ô][Ô]>32</int2></HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>[Ô]);
return soapEnvelop;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
Tópico encerrado , respostas não são mais permitidas