How to access a protected webservice ?

how to access a protected webservice ? here I am explaining the two ways to access protect our webservices. 1) access ‘ X509Certificate ‘ protectd webservice 2) access ‘username token’ protected webservices. Find the below given code to understand more, think that the webservice url “https://XXXXXXXXXXXXXXXXXXXXXXX” is protected by x509cerificate as well as ‘username token’ let us see how can we access this service.


Private Sub callWS()
Dim strerrorCode, strerrorDesc, test As String
Dim strCertPath, strCertPassword As String
Dim objProxy As New proxy ‘—– new proxy class
Dim objDisclosureResponse As New MQJob_DisclosurePush.pushDisclosureResponse
Try
Dim strWsdlUrl = “https://XXXXXXXXXXXXXXXXXXXXXXx”‘—–To get the webservice URL
Dim strDBUrl As String
Dim objCert As X509Certificate2
strCertPath = “c:\cert “‘—–To get the certificate path
strCertPassword = “pswd” ‘—–To get the certificate password
If strCertPath <> “” AndAlso File.Exists(strCertPath) Then
objCert = New X509Certificate2(strCertPath, strCertPassword)
objProxy.ClientCertificates.Add(objCert)
objCert = Nothing
End If

objProxy.Url = strWsdlUrl
objProxy.RequestSoapContext.Security.Tokens.Add(GetUserNameToken())
Dim objResponce As New PROXYOBJ ‘—- create object for the main class which is there in proxyclass
objResponce = objProxy.method(input object)
If (Not objResponce .xxxx Is Nothing) Then
// success
Else
// failure
End If

Catch ex As Exception
‘ Code for Exception handling
objLogger.WriteBody(“— Exception details : ” & ex.ToString() & ” — “)

End Try
objProxy = Nothing
objDisclosureResponse = Nothing

End sub


Private Function GetUserNameToken() As UsernameToken
Dim strUsrNm As String = “test”
Dim strPswd As String = “&HDNDODMDODKPKP”‘———–encripted pswd
Dim token As New UsernameToken(strUsrNm, strPswd, PasswordOption.SendPlainText)
Return token
End function

thanks !

 

Leave a Reply

Your email address will not be published. Required fields are marked *