- I have attempted creating a simple asp.net webservice
- Testing the same using Httpwebrequest POST
- Trying same example for WCF Service
- Implement Data Driven Testing for this example using Nunit
- Logging and Reporting
Step 2 - Below sample project would be created, Modify the code for addition of two numbers
[WebMethod]
public int AddNumbers(int a, int b)
{
return a+b;
Step 4 - Created a Console Application
Copy - Paste below code in the Console App. Provide the correct modified URL for the webapp.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;
using System.IO;
namespace WebServiceAutomation
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Calling AddSum webmethod");
int input1 = 10;
int input2 = 20;
int expectedresult = 30;
int actualresult;
string postData = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<AddNumbers xmlns=""http://tempuri.org/"">
<a>input1</a>
<b>input2</b>
</AddNumbers>
</soap:Body>
</soap:Envelope>";
postData = postData.Replace("input1",input1.ToString());
postData = postData.Replace("input2",input2.ToString());
byte[] buffer = Encoding.ASCII.GetBytes(postData);
string url = "http://localhost:1033/Service1.asmx?op=AddNumbers";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "text/xml";
req.ContentLength = buffer.Length;
req.Timeout = 5000;
Stream request = req.GetRequestStream();
request.Write(buffer, 0, buffer.Length);
request.Flush();
request.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream resst = res.GetResponseStream();
StreamReader sr = new StreamReader(resst);
string response = sr.ReadToEnd();
Console.WriteLine("HTTP Response is " + response);
Console.ReadKey();
int start = response.IndexOf("<AddNumbersResult>");
int end = response.IndexOf("</AddNumbersResult>");
string Actualresult = response.Substring(start+18, end - start-18);
Console.WriteLine("Expectd Result is " + expectedresult +"\n");
Console.WriteLine("Actual Result is " + Actualresult + "\n");
Console.ReadKey();
}
}
}
Step 5 - Output Window After executing the Console App
Explanation for the code
- If you want to interact more directly with a Web server, the HttpWebRequest and HttpWebResponse add additional methods that make this easier
- HttpWebRequest and HttpWebResponse class is inside the System.NET namespace, and this two classes is designed to communicate by using the Http Protocol
- HttpWebRequest can be used to send HTTP request to the server
- Stream Class – msdn link
- The Stream class is abstract
Means that same code can be used to read from or write to any kind of secondary storage (aka the backing store) - The StreamWriter class is derived from an abstract class called TextWriter, which writes characters to a stream or file
The StreamReader class is derived from an abstract class called TextReader, which reads characters from a stream or file - StreamWriter is quite a bit different than StreamReader
- StreamReader converts text to string or char[ ]
- It does not do "text to numeric/object" conversions
- You have to manually parse and convert the input
- StreamWriter automatically converts all types of binary data to human-readable text for display
- Stream Reader
- Read a line from the file using the ReadLine() method
- Returns a string if OK, null on end-of-file
- Stops reading when newline or "\r\n" encountered
- End-of-line is discarded
- Read entire file into a single string using ReadToEnd()
- Learn Basics of how web services work - Anatomy of an XML Web Service Lifetime
- How To: Create a “Hello World” WCF Service Using Visual Studio
- Call WCF Webservice from Windows Application using HTTPWebRequest
- A Cleaner - Soap based Web Service & WCF Test Tool
- WCF Load Test Version 3.0 (Beta) for Visual Studio 2010
- How to Create a WCF Client for ASMX Web Service Without Using Web Proxy
- Automate Your ASP.NET Web Services Testing
- wizdl - Web Service GUI Test Tool
- Creating a Data-Driven Web Test against a Web Service
- Generate thousand of request using httpwebrequest
- Call XML webservice through c#.net
SOAP Vs REST (Representative State Transfer (REST))
- REST stands for Representational State Transfer, this basically means that each unique URL is a representation of some object
- It embraces a stateless client-server architecture in which the web services are viewed as resources and can be identified by their URLs. Source - Link
- REST has no WSDL interface definition
- REST is over HTTP, but SOAP can be over any transport protocols such HTTP, FTP, STMP, JMS etc.
- SOAP is using soap envelope, but REST is just XML
- SOAP RPC: Verb based. GetUser(string userId), AddUser(User u)
- REST: Noun based. Ex- http://mysite/users/dseven, http://mysite/users/newUserForm
- REST does NOT require a separate page for each data item, just a separate address.
Good Read - REST vs. SOAP – The Right WebService
Soap Headers Authentication in Web Services
CodeSnip: Handling SOAP Exceptions in Web Services
Interview Question: Compare two web services type SOAP and RESTful (SOAP Vs RESTful)
XML comparison tutorial using XMLUnit
Consuming Twitter APIs
Happy Reading!!