This is an example for how to consume the asp.net webservice method which will return the bytes of array as datatype.
How it will be consumed in PHP client is slightly different one.The returned xsd schema data type will be not as byte as we had in ASP.NET service rather encoded base64 string.
So we need to decode the base64 string as plain text.After we can write back to stream or screen.
ASP.NET webservice code for Base64Binary type
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service ()
{
}
[WebMethod]
public byte[] ShowFileContent()
{
FileStream fs = File.OpenRead(Server.MapPath("Resume.doc"));
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
return b;
}
}
PHP Client:
using nusoap webservice class.really its very handy.so my code length shrink about to 4 or 5 lines.
require_once('lib/nusoap.php');
$client = new soapclient('http://localhost/website2/Service.asmx?WSDL','wsdl');
$result = $client->call(ShowFileContent);
$r=array($result['ShowFileContentResult']);
print_r(base64_decode($r[0]),$handle);
//here you can write into file also.
?>
Monday, April 20, 2009
Tuesday, April 14, 2009
asp.net webservice SoapHeader Authentication
After long time,this is my second attempt to authenticate the webservice using SoapHeader.My System Architect team asked me to work aroud on this issue.Its works atlast :)-
SoapAuthenticateService.cs
*******************************
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(Name = "TestService", ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SoapAuthenticateService : System.Web.Services.WebService
{
public UserAuthorise name;
public SoapAuthenticateService()
{
}
[WebMethod]
[SoapDocumentMethod(Binding = "TestService")]
[SoapHeader("name")]
public string GetString()
{
if (checkUser())
{
return "Authenticated Successfully";
}
else
{
return " You are not authorised yet !";
}
}
public bool checkUser()
{
if (name.userName == "Murugesan" && name.password == "12345")
{
return true;
}
else
{
return false;
}
}
}
________________________________________________________________________________
UserAuthorise.cs
**************************
public class UserAuthorise : System.Web.Services.Protocols.SoapHeader
{
public string userName;
public string password;
}
__________________________________________________________________________________
Service.asmx
***********************
<%@ WebService Language="C#" CodeBehind="~/App_Code/SoapAuthenticateService.cs" Class="SoapAuthenticateService" %>
Consumer Application code :
SoapAuthenticateService ap = new SoapAuthenticateService();
UserAuthorise u = new UserAuthorise();
u.userName = "Murugesan";
u.password = "12345";
ap.UserAuthoriseValue = u;
Response.Write(ap.GetString());
instead of hard coded user name and password,you can use the DB or XML as datasource.
SoapAuthenticateService.cs
*******************************
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(Name = "TestService", ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SoapAuthenticateService : System.Web.Services.WebService
{
public UserAuthorise name;
public SoapAuthenticateService()
{
}
[WebMethod]
[SoapDocumentMethod(Binding = "TestService")]
[SoapHeader("name")]
public string GetString()
{
if (checkUser())
{
return "Authenticated Successfully";
}
else
{
return " You are not authorised yet !";
}
}
public bool checkUser()
{
if (name.userName == "Murugesan" && name.password == "12345")
{
return true;
}
else
{
return false;
}
}
}
________________________________________________________________________________
UserAuthorise.cs
**************************
public class UserAuthorise : System.Web.Services.Protocols.SoapHeader
{
public string userName;
public string password;
}
__________________________________________________________________________________
Service.asmx
***********************
<%@ WebService Language="C#" CodeBehind="~/App_Code/SoapAuthenticateService.cs" Class="SoapAuthenticateService" %>
Consumer Application code :
SoapAuthenticateService ap = new SoapAuthenticateService();
UserAuthorise u = new UserAuthorise();
u.userName = "Murugesan";
u.password = "12345";
ap.UserAuthoriseValue = u;
Response.Write(ap.GetString());
instead of hard coded user name and password,you can use the DB or XML as datasource.
Consuming ASP.NET webservice through PHP
I am trying to get least familiar with PHP code.I tried this simple asp.net webservice which is going to be consumed through PHP.
My asp.net code snippet
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(string name) {
return "Hello " + name;
}
}
_________________________________________________________________________
PHP
_________________________________________________________________________
require_once('lib/nusoap.php');
$objClient = new soapclient("http://localhost/website1/Service.asmx?WSDL");
$result = $objClient->call("HelloWorld", array('name' => 'Murugesan'));
echo $result;
?>
After i tried neckling fight with PHP code,I did not get my code works as I expected.
I keep my fingers cross over where I am doing wrong.
My asp.net code snippet
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(string name) {
return "Hello " + name;
}
}
_________________________________________________________________________
PHP
_________________________________________________________________________
require_once('lib/nusoap.php');
$objClient = new soapclient("http://localhost/website1/Service.asmx?WSDL");
$result = $objClient->call("HelloWorld", array('name' => 'Murugesan'));
echo $result;
?>
After i tried neckling fight with PHP code,I did not get my code works as I expected.
I keep my fingers cross over where I am doing wrong.
Subscribe to:
Posts (Atom)