Table of Contents
Overview:
Create the call to the web service and have it return a set of data or object.
Creating a Post Method
private XmlDocument CallWebServicePost(string method, string operation, string xmlPayload)
{
string result = "";
string CREDENTIALS = "PASSword123";
string URL_ADDRESS = "http://web-01:8083/WPAddIn.asmx/GetAllCompanies";
//string URL_ADDRESS = "http://www.client.com/_ws/" + method + "?sso=" + CREDENTIALS + "&o=" + operation +; //TODO: customize to your needs
// ===== You shoudn't need to edit the lines below =====
// Create the web request
HttpWebRequest request = WebRequest.Create(new Uri(URL_ADDRESS)) as HttpWebRequest;
// Set type to POST
request.Method = "POST";
request.ContentType = "application/xml";
// Create the data we want to send
StringBuilder data = new StringBuilder();
data.Append(xmlPayload);
byte[] byteData = Encoding.UTF8.GetBytes(data.ToString()); // Create a byte array of the data we want to send
request.ContentLength = byteData.Length; // Set the content length in the request headers
// Write data to request
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
// Get response and return it
XmlDocument xmlResult = new XmlDocument();
try
{
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
reader.Close();
}
xmlResult.LoadXml(result);
}
catch (Exception e)
{
var debug = 1;// xmlResult = CreateErrorXML(e.Message, ""); //TODO: returns an XML with the error message
}
return xmlResult;
}
Create a Get Request
private XmlDocument CallWebServiceGet()
{
string result = "";
string CREDENTIALS = "PASSword123";
string URL_ADDRESS = "http://lweb-01:8083/WPAddIn.asmx/GetAllCompanies";
// ===== You shoudn't need to edit the lines below =====
XmlDocument xmlResult = new XmlDocument();
//creating the proxy for the service call using the HttpWebRequest class
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(URL_ADDRESS);
//Set the method/action type
WebReq.Method = "GET";
//We use form contentType
WebReq.ContentType = "text/xml; charset = utf - 8";
// WebReq.ContentType = "application/xml";
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//Now, we read the response (the string), and output it.
Stream MyResponse = WebResp.GetResponseStream();
//read the stream into streamreader
StreamReader MyResponseReader = new StreamReader(MyResponse);
//Loading into XML doc
//xmlResult = Xml
XDocument xmlDoc = XDocument.Load(MyResponseReader);
//read the XML nodes using Linq
var itemList = from content in xmlDoc.Descendants("item")//node
select new
{
itemName = content.Element("itemName ").Value,
categoryName = content.Element("categoryName").Value,
itemImage = content.Element("itemImage").Value,
};
return xmlResult;
}
Resources:
https://swagatblog.wordpress.com/2011/01/09/consume-web-services-dynamically-using-httpwebrequest/
https://code.msdn.microsoft.com/Dynamic-web-service-call-0512a318
www.diogonunes.com/blog/calling-webservice-without-wsdl-or-web-reference/
Tags: WebServices Webservice Web Services