Friday, June 21, 2013

REsT Services

using System.ServiceModel;

using System.ServiceModel.Web;

namespace RestService



{


 
[ServiceContract]

public interface IRestServiceImpl



{
 
[OperationContract]

[WebInvoke(Method = "POST",

ResponseFormat = WebMessageFormat.Xml,

BodyStyle = WebMessageBodyStyle.Wrapped,

UriTemplate = "xml/{id}")]

string XMLData(string id);

[OperationContract]

[WebInvoke(Method = "GET",

ResponseFormat = WebMessageFormat.Json,

BodyStyle = WebMessageBodyStyle.Wrapped,

UriTemplate = "json/{id}")]

string JSONData(string id);

[OperationContract]

[WebInvoke(Method = "POST",

ResponseFormat = WebMessageFormat.Xml,

RequestFormat = WebMessageFormat.Xml,

BodyStyle = WebMessageBodyStyle.Bare,

UriTemplate = "auth")]

ResponseData Auth(RequestData rData);



}

}
 

using System.ServiceModel.Activation;
namespace RestService
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string XMLData(string id)
{
return "You requested product " + id;
}
public string JSONData(string id)
{
return "You requested product " + id;
}
public ResponseData Auth(RequestData rData)
{
// Call BLL here
var data = rData.details.Split('|');
var response = new ResponseData
{
Name = data[0],
Age = data[1],
Exp = data[2],
Technology = data[3]
};
return response;
}
#endregion
 
 

}
}
 
 




using System;

using System.IO;

using System.Net;

using System.Web.UI;

using System.Xml;

using System.Text;

namespace WebClient



{

public partial class _default : Page



{

protected void Page_Load(object sender, EventArgs e)



{

WebxmlResponse();

// WebJsonResponse();



}

protected void Button1_Click(object sender, EventArgs e)



{

WebXmlResponse();

}

public void WebXmlResponse()



{

HttpWebRequest req = null;

HttpWebResponse res = null;

try



{

const string url = "http://localhost:35798/RestServiceImpl.svc/auth";

req = (HttpWebRequest)WebRequest.Create(url);

req.Method = "POST";

req.ContentType = "application/xml; charset=utf-8";



req.Timeout = 30000;

req.Headers.Add("SOAPAction", url);

var xmlDoc = new XmlDocument { XmlResolver = null };

xmlDoc.Load(Server.MapPath("PostData.xml"));

string sXml = xmlDoc.InnerXml;



req.ContentLength = sXml.Length;

var sw = new StreamWriter(req.GetRequestStream());



sw.Write(sXml);

sw.Close();

res = (HttpWebResponse)req.GetResponse();

Stream responseStream = res.GetResponseStream();

var streamReader = new StreamReader(responseStream);

//Read the response into an xml document

var soapResonseXmlDocument = new XmlDocument();



soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());

//return only the xml representing the response details (inner request)



TextBox1.Text = soapResonseXmlDocument.InnerXml;

//Response.Write(soapResonseXMLDocument.InnerXml);



}

catch (Exception ex)



{

Response.Write(ex.Message);

}

}

public void WebJsonResponse()



{

string url = "http://localhost:35798/RestServiceImpl.svc/json/1";

string strResult = string.Empty;

HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);

webrequest.Method = "POST";

webrequest.ContentType = "application/json; charset=utf-8";

HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

Encoding enc = Encoding.UTF8;

StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);



strResult = loResponseStream.ReadToEnd();

loResponseStream.Close();

webresponse.Close();

TextBox1.Text = strResult;

}

public void WebxmlResponse()



{

string url = "http://localhost:35798/RestServiceImpl.svc/xml/1";

string strResult = string.Empty;

HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);

webrequest.Method = "POST";

webrequest.ContentType = "application/xml; charset=utf-8";

HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();

Encoding enc = Encoding.UTF8;

StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);



strResult = loResponseStream.ReadToEnd();

loResponseStream.Close();

webresponse.Close();

TextBox1.Text = strResult;

}

}

}


No comments:

Post a Comment