How to call a web service using HttpWebRequest?

How to call a web method on a web service using HTTPWebRequest?

  • When using an HTTPWebRequest object to call a web service, and supply the wsdl parameter, I get the description page ok. Dim req As Net.HttpWebRequest Dim resp As Net.HttpWebResponse Dim sr As IO.StreamReader Dim text As String req = Net.WebRequest.Create("http://www.mysite.com/DS.asmx?wsdl") ' works ' resp = req.GetResponse sr = New IO.StreamReader(resp.GetResponseStream) text = sr.ReadToEnd However, when I attempt to call one of the methods using the reference showing as the soapAction, I get (404) Not Found. req = Net.WebRequest.Create("http://mysite.com/DS/StaffList") ' fails ' Is there a difference in the way a method should be called that I'm missing? Am I misinterpreting the structure of the address? Note: The service works fine if I set a standard WebReference in the Visual Studio project and call the method on the WebService object, so the service itself is available and working.

  • Answer:

    In order to invoke a SOAP web service you will need to POST a http://en.wikipedia.org/wiki/SOAP. That's what the generated classes using WebReference do behind the scenes. Unless you have a compelling reason you should never invoke a SOAP web service using the low level classes such as HttpWebRequest but generate a proxy from the WSDL using Add WebReference. Take a look at http://msdn.microsoft.com/en-us/library/ms972326.aspx. Also note that classic ASMX web services is now considered a deprecated technology and should be replaced by http://msdn.microsoft.com/en-us/netframework/aa663324.aspx.

Bill at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

You are trying a pre-WCF web service. If migrating to a WCF service is an option, you can map the service to http://stackoverflow.com/questions/3095736/customize-wcf-service-endpoint-address-in-iis-host. http://en.wikipedia.org/wiki/Representational_State_Transfer (Representational State Transfer) is used (among other options) to describe function calls in URL structures, without additional content such as SOAP.

gimel

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.