Web API Accept XML With Header Authentication

Pada posting Web API Accept XML With Header Authentication akan dibahas cara membuat Web API untuk menerima inputan berupa data XML yang diproteksi dengan user dan password yang ada di HTTP Header.

Di contoh berikut ini akan diperlihatkan cara pembuatan dengan memanfaatkan Generic Handler di ASP.NET yang di-mapping ke halaman Default.

Dibawah ini mapping di web.config


<system.web>    

<urlMappings enabled="true">
  <add url="~/Default.aspx" mappedUrl="~/Handler.ashx"/>
</urlMappings>

</system.web>

Kemudian tambahkan item Generic Handler di Web Project

Web API Accept XML With Header Authentication


Dibawah ini adalah isi dari File Handler.asx

<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Web
Imports System.IO
Imports System.Xml

Public Class Handler : Implements IHttpHandler 
    Public Sub ProcessRequest(ByVal oCtx As HttpContext) Implements IHttpHandler.ProcessRequest

        Try
            Dim oXMLContent As String = New StreamReader(oCtx.Request.InputStream).ReadToEnd()
            Dim oXMLResponse As XmlDocument = New XmlDocument
            oXMLResponse.LoadXml(oXMLContent)
           
            oCtx.Response.ContentType = "text/xml"
            Dim sUser As String = oCtx.Request.Headers.Get("x-user")
            Dim sPwd As String = oCtx.Request.Headers.Get("x-password")

            If sUser = Nothing Then
                Dim sXMLErr As String = "<?xml version=""1.0"" encoding=""iso-8859-1""?>" & _
                                        "<!DOCTYPE Order SYSTEM ""https://qapartsb2bwsx.rd.cat.com/psb2b/dtd/Order.dtd""[]>" & _
                                        "<Failed>USER is null</Failed>"
                oCtx.Response.Write(sXMLErr)
            Else
                oCtx.Response.Write(oXMLContent)
            End If

        Catch ex As Exception
            oCtx.Response.Write("SERVER " & ex.Message)
        End Try
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class


Semoga posting tentang Web API Accept XML With Header Authentication ini dapat bermanfaat.



Salam,

Popular posts from this blog

SmartObject property ID is a required property for selected method Create. Value must be set.

Cara inject USB 3.0 Driver pada instalasi Windows - How to Inject USB 3.0 Driver in Windows 7

Python Font Color in Console