2008/Mar/26

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-874" />
<title>Web Service - Currency</title>
<script type="text/javascript">
function Init_AJAX() {
 try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
 try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
 try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
 alert("XMLHttpRequest not supported");
 display("Init Object");

 return null;
}

function startHttp() {
 var xmlHttp = Init_AJAX();
 xmlHttp.onreadystatechange = function () {
 if (xmlHttp.readyState==4) {
  if (xmlHttp.status==200) {
   display(xmlHttp.responseText); //แสดงเนื้อหาของส่วนนั้นๆ
  }
  else {
   wait();
  }
 }
 };
 xmlHttp.open("GET", "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=THB", true);
 xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); // set Header
 xmlHttp.send(null); //ส่งค่า
}

function wait()
{
 document.getElementById("Status").innerHTML = document.getElementById("Status").innerHTML + "." ;
}

function display(strText)
{
 document.getElementById("News").innerHTML = strText;
 document.getElementById("Status").innerHTML = "";
}
</script>
</head>

<body>
<tr>
<td>
<input type="button" value="  Submit  " onclick="startHttp();">
<script language="javascript" type="text/javascript">
window.onload = function() {
startHttp();
}
</script>

<div id="News"></div>
</td>
<td>
<div id='status'></div>
</td>
</tr>
</body>
</html>

2007/Aug/31

===== 1. Web Service (WSAuthHeader.asmx) =====

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

Namespace VBService
Public Class AuthHeader
Inherits SoapHeader
Public Username As String
Public Password As String
End Class

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WSAuthHeader
Inherits System.Web.Services.WebService
Public sHeader As AuthHeader

<WebMethod(), SoapHeader("sHeader")> _
Public Function SecureMethod() As String
If (sHeader Is Nothing) Then
Return "ERROR : Please supply credentials"
End If
Dim usr As String = sHeader.Username
Dim pwd As String = sHeader.Password
If (AuthenticateUser(usr, pwd)) Then
Return "SUCCESS :" & usr
Else
Return "ERROR:Could not authenticate"
End If
End Function
Private Function AuthenticateUser(ByVal usr As String, ByVal pwd As String) As Boolean
If (Not (usr Is Nothing) And Not (pwd Is Nothing)) Then
Return True
End If
Return False

End Function
End Class
End Namespace


===============================

====== 2. Test Authentication from Header SOAP on Web Service (TestWSAuth.aspx) =====

Partial Class TestWSAuth
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write("<font face=""verdana""><h4>Using Soap Headers for Custom Authentication</h4>")
Dim proxy As New WSAuth.WSAuthHeader
proxy.Url = "
http://localhost/WebShowData/WSAuthHeader.asmx"
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials

Response.Write("<h5>First call result without SOAP Header:</h5>")
Try
Response.Write("<p>")
Response.Write(proxy.SecureMethod())
Response.Write("</p>")
Catch ex As Exception
Response.Write("<pre>")
Response.Write(ex.StackTrace)
Response.Write("</pre>")
End Try
Dim myHeader As New WSAuth.AuthHeader
myHeader.Username = "JaneDoe"
myHeader.Password = "password"
proxy.AuthHeaderValue = myHeader
Response.Write("<h5>Second call result with SOAP Header:</h5><p>" & proxy.SecureMethod() & "</p></font>")
End Sub
End Class

===============================

======= 3. SOAP XML Request =====

POST /WebShowData/WSAuthHeader.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "
http://tempuri.org/SecureMethod"

<?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:Header>
<AuthHeader xmlns="
http://tempuri.org/">
<Username>AAA</Username>
<Password>BBB</Password>
</AuthHeader>
</soap:Header>
<soap:Body>
<SecureMethod xmlns="
http://tempuri.org/" />
</soap:Body>
</soap:Envelope>

===============================

======= 4. SOAP XML Response =====
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="
http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SecureMethodResponse xmlns="
http://tempuri.org/">
<SecureMethodResult>SUCCESS :AAA</SecureMethodResult>
</SecureMethodResponse>
</soap:Body>
</soap:Envelope>
===============================


edit @ 2007/08/31 13:58:34