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

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



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