Consuming Web Service without Reference

by Mohit Bhardwaj 3. May 2009 02:29

Hello folks!!!!!!!!!!!

I am back. From the last 2 days I have been working on SOAP Protocol because there was a requirement in my Project that I have to access a webservice by its URL. So that means, I could not create a web reference of that web service to add in my project. So, I decided to use SOAP protocol, but still I ran into problems in the SOAP headers. Then, I ran into a post of one my very good friend

Vinit Jain and his post on that

http://blog.codergenie.com/post/Call-a-webservice-dynamically-without-webreference.aspx.

Now, it cleared some concepts to use a webservice using HTTP POST method, because its nothing but a simple WebResponse in XML format. So, I have this function Verify in my webservice which takes two parameters, A &  B. Thus,

<WebMethod()> _

Public Function Verify(ByVal AccountNumber As String, ByVal ProductName As String) As String

Return "Allowed"

End Function

Thus it will return "Allowed". Now, I was still having problem in the method my friend Vinit was suggesting and that was I was getting 500 Internal Server Error. And it, usually comes when a webrequest is not availabel. So, again I gone into RnD digging into the things on how to resolve it. Then somewhere, I found that to enable webservice to listen HTTP POST request you have to turn on the HTTP POST verb in the webservice's Web Config. So that means, In <system.web>

<customErrors mode="Off"/><

webServices>

<protocols>

<add name="HttpPost"/> </protocols>

</webServices>

This will enable the POST verb in the webservice. But somehow, now the Response of the webrequest is getting stuck. So I created a new method to work on this issue. Now a webservice with Parameters can be accessed via URL we all know that. Like in this case, http://consultflux.com/Verify/Verification.asmx/Verify?AccountNumber=3223&ProductName=876

Now in the coding part what I did is,

Try

Dim str As String = ""

Dim objUrl As New System.Uri("http://consultflux.com/Verify/Verification.asmx/Verify?AccountNumber=3223&ProductName=876")

Dim objWebReq As System.Net.WebRequest

Dim objResp As System.Net.WebResponse

Dim sReader As System.IO.StreamReader

Try

objWebReq = System.Net.WebRequest.Create(objUrl)

objResp = objWebReq.GetResponse

sReader =
New System.IO.StreamReader(objResp.GetResponseStream)

str = sReader.ReadToEnd.ToString()

sReader.Close()

objResp.Close()

Catch ex As Exception

str = ex.ToString()

Finally

objWebReq = Nothing

End Try

Return str

Catch ex As Exception

Return ex.ToString()

End Try

Now what happens here is we create a webrequest on that specified URL or URI to be exact, and get the response back to us. We then use the response to read by streamreader and put all the data in a string. The data will come in XML format as:

<?xml version="1.0" encoding="utf-8" ?><string xmlns="http://tempuri.org/">Allowed</string>
And this is what we wanted. We accessed a remote webservice via its URL and its function parameters and got our result back. Now their is some limitation,
  • The URL property onlt allow 255 characters with no spaces
  • It may not be very secure.

So, I think we are done here. Thanks for taking a look.

Disclaimer: All these posts are my personal thoughts and understandings developed while working. If you need to use any code from my post, please, do it at your own risk. I shall not be held responsible for any kind of security or data risk you may or may not face.

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

No Category

Sending Email using ASP.NET [PART 2]

by Mohit Bhardwaj 4. April 2009 12:19

Last time I have added a post on Sending email using Gmail. Now, I have been working on a project which needs to send emails to the different user's using Godaddy's smtp service. But problem was that I can not send emails to the godaddy server by using its default smtp address as smtpout.secureserver.net at port 80 from the webhosting server. After googling I found out that this smtp is an outside smtp service of godaddy which is used to send emails coming from outside of its server environment. But it will not work when the application is hosted inside of the server environment, which was the case I was having. So, I found out that the correct smtp server I should use from inside the webshosting server is relay-hosting.secureserver.net at port 25.

Now, I assumed that my problem has been resolved. But now here comes the actuall problem. Every time I needed to work on the project I had to change the smtp and port number to smtpout.secureserver.net instead of relay-hosting.secureserver.net because it is in inside relaying smtp hosts so it is not accessible from the outside world. And this has become many times a real problem as I usually forgets to update the settings again and upload it back on the server and then email doesn't work. CryCry

Then I came up with a trick to get the smtp and port number automatically by its own and here is how I did it.

I have created a file name Mail.xml. Save this file on your hosting service's Virtual directory which are you using on the internet.

<?xml version="1.0" encoding="utf-8" ?>

- <mail>
  <setting host="relay-hosting.secureserver.net" port="25" />
</mail>
And, we will create a second version of this xml file which will be saved in our local develoment PC i.e at the wwwroot/inetpub/project directory.

<?xml version="1.0" encoding="utf-8" ?>

- <mail>
  <setting host="smtpout.secureserver.net" port="80" />
</mail>
Then I have created a class file to introduce the functionality of getting the configuration and then send emails.

Imports Microsoft.VisualBasic

Imports System.Net

Imports System.Net.Mail

Imports System.Net.Configuration

Imports System.Xml

 

Public Class ModuleClass

Public function sendmail(ByVal body As String, ByVal too As String, ByVal subject As String) As String

Try

Dim arr As New ArrayListDim xtr As XmlTextReader = New XmlTextReader(System.Web.HttpContext.Current.Server.MapPath("Mail.xml"))

xtr.WhitespaceHandling = WhitespaceHandling.None

Dim xd As XmlDocument = New XmlDocument

xd.Load(xtr)

Dim xnodRoot As XmlNode = xd.DocumentElement

Dim xnodWorking As XmlNode

If xnodRoot.HasChildNodes Then

xnodWorking = xnodRoot.FirstChild

While Not IsNothing(xnodWorking) arr = ProcessChildren(xnodWorking, 0)

' ProcessChildren is a function that will return an arraylist with first parameter as server name and second as port number.

xnodWorking = xnodWorking.NextSibling

End While

End If

Dim mail As New MailMessage

mail.To.Add(New MailAddress(too))

' I am hardcoding here that email will be sent by xxxx@abc.com , if you want you can add one more parameter to the function.

mail.From =
New MailAddress("xxxx@abc.com")mail.Bcc.Add(New MailAddress("yyyy@123.com"))

mail.Body = body

mail.Subject = subject

Dim client As SmtpClient = New SmtpClient(arr(0).ToString(), CInt(arr(1).ToString()))

client.DeliveryMethod = SmtpDeliveryMethod.Network

client.Credentials =
New System.Net.NetworkCredential(mail.From.Address.ToString(), "^&%&^%&^%")

' this is the password of the account used in the from field of the email. Remember this must be a valid account and it should have the rights to send emails.

client.Send(mail)

Return "Success"

Catch ex As Exception

Return "Failed"

End Try

End Function

 

Private Function ProcessChildren(ByVal xnod As XmlNode, ByVal Depth As Integer) As ArrayList Dim arr As New ArrayList

Dim strNode As String

Dim intI, intJ As Integer

Dim atts As XmlAttributeCollection

'we're only going to process Text and Element nodes

If (xnod.NodeType = XmlNodeType.Element) Or (xnod.NodeType = XmlNodeType.Text) Then

strNode = ""

'attributes too

atts = xnod.Attributes

If Not atts Is Nothing Then

For intJ = 0 To atts.Count - 1

strNode = ""

For intI = 1 To Depth + 1

strNode &= "-"

Next

arr.Add(atts("host").Value.ToString()) arr.Add(atts("port").Value.ToString())

Next

End If

End If

Return arr

End Function

End Class

What happens now is that whenever you tries to send the email, it will load the configuration from the xml file in both the PC and server environment and then you can send email from both of them.

[Note: I have googled this code but I am not aware of the person who wrote this code of getting things from XML file. So I am hereby giving credit to that person who created this function. Thanks Man.]

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Beginners | No Category

First Blog, First Entry

by Mohit Bhardwaj 12. March 2009 02:55

Hello to all,

This is my first blog entry ever and I am very much excited about this. So, please spend some time on my blog and see if you like something.

I hope you won't be disappointed.

Thanks.

Mohit Bhardwaj.

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

No Category

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About Me

Hi, I am Mohit Bhardwaj, living in New Delhi, India. I am an Independent software developer for Microsoft Technologies.

Hope you will like my posts here on this blog.

LinkedIn

Page List

    Calendar

    <<  March 2010  >>
    MoTuWeThFrSaSu
    22232425262728
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    View posts in large calendar