consume web service via httpwebrequest

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hharry

    #1

    consume web service via httpwebrequest

    hello all,

    trying to consume a simple web service using httpwebrequest instead of
    generating a proxy class.

    code for simple web service:

    Imports System.Web.Serv ices

    <System.Web.Ser vices.WebServic e(Namespace :=
    "http://tempuri.org/SoapServer/Service1")_
    Public Class Service1
    Inherits System.Web.Serv ices.WebService

    <WebMethod()_
    Public Function HelloWorld() As String
    Return "Hello World"
    End Function
    End Class

    code to consume web service using httpwebrequest object:

    Dim sTmp As String
    sTmp = "" & _
    "<?xml version=" + Chr(34) + "1.0" + Chr(34) + " encoding="
    + Chr(34) + "utf-8" + Chr(34) + "?>" & _
    "<soap:Enve lope xmlns:xsi=" + Chr(34) +
    "http://www.w3.org/2001/XMLSchema-instance" + Chr(34) + " xmlns:xsd=" +
    Chr(34) + "http://www.w3.org/2001/XMLSchema" + Chr(34) + " xmlns:soap="
    + Chr(34) + "http://schemas.xmlsoap .org/soap/envelope/" + Chr(34) + ">"
    & _
    "<soap:Body >" & _
    "<HelloWorl d xmlns=" + Chr(34) +
    "http://tempuri.org/SoapServer/Service1" + Chr(34) + " />" & _
    "</soap:Body>" & _
    "</soap:Envelope>"

    objHttpRequest =
    WebRequest.Crea te("http://localhost/SoapServer/Service1.asmx")
    objHttpRequest. Method = "POST"
    objHttpRequest. ContentType = "text/xml"
    objHttpRequest. ContentLength = sTmp.Length
    objHttpRequest. Headers.Add("SO APAction",
    "http://tempuri.org/SoapServer/Service1/HelloWorld")

    myWriter = New StreamWriter(ob jHttpRequest.Ge tRequestStream( ))
    myWriter.Write( sTmp)

    objHttpResponse = objHttpRequest. GetResponse
    objResponseStre am = New
    StreamReader(ob jHttpResponse.G etResponseStrea m())
    sResult = objResponseStre am.ReadToEnd
    objResponseStre am.Close()

    =============== =============== =============== =============== =====
    =============== =============== =============== =============== =====

    the code fails at objHttpResponse = objHttpRequest. GetResponse, an
    exception is thrown stating the request timed out.

    pointers appreciated..

    thanks in advance

  • tommaso.gastaldi@uniroma1.it

    #2
    Re: consume web service via httpwebrequest

    Make sure to use as namespace your domain and make
    sure the server, where the service is hosted, is up and running (try
    to load directly the asmx to see if it works) ...

    -t

    hharry ha scritto:
    hello all,
    >
    trying to consume a simple web service using httpwebrequest instead of
    generating a proxy class.
    >
    code for simple web service:
    >
    Imports System.Web.Serv ices
    >
    <System.Web.Ser vices.WebServic e(Namespace :=
    "http://tempuri.org/SoapServer/Service1")_
    Public Class Service1
    Inherits System.Web.Serv ices.WebService
    >
    <WebMethod()_
    Public Function HelloWorld() As String
    Return "Hello World"
    End Function
    End Class
    >
    code to consume web service using httpwebrequest object:
    >
    Dim sTmp As String
    sTmp = "" & _
    "<?xml version=" + Chr(34) + "1.0" + Chr(34) + " encoding="
    + Chr(34) + "utf-8" + Chr(34) + "?>" & _
    "<soap:Enve lope xmlns:xsi=" + Chr(34) +
    "http://www.w3.org/2001/XMLSchema-instance" + Chr(34) + " xmlns:xsd=" +
    Chr(34) + "http://www.w3.org/2001/XMLSchema" + Chr(34) + " xmlns:soap="
    + Chr(34) + "http://schemas.xmlsoap .org/soap/envelope/" + Chr(34) + ">"
    & _
    "<soap:Body >" & _
    "<HelloWorl d xmlns=" + Chr(34) +
    "http://tempuri.org/SoapServer/Service1" + Chr(34) + " />" & _
    "</soap:Body>" & _
    "</soap:Envelope>"
    >
    objHttpRequest =
    WebRequest.Crea te("http://localhost/SoapServer/Service1.asmx")
    objHttpRequest. Method = "POST"
    objHttpRequest. ContentType = "text/xml"
    objHttpRequest. ContentLength = sTmp.Length
    objHttpRequest. Headers.Add("SO APAction",
    "http://tempuri.org/SoapServer/Service1/HelloWorld")
    >
    myWriter = New StreamWriter(ob jHttpRequest.Ge tRequestStream( ))
    myWriter.Write( sTmp)
    >
    objHttpResponse = objHttpRequest. GetResponse
    objResponseStre am = New
    StreamReader(ob jHttpResponse.G etResponseStrea m())
    sResult = objResponseStre am.ReadToEnd
    objResponseStre am.Close()
    >
    =============== =============== =============== =============== =====
    =============== =============== =============== =============== =====
    >
    the code fails at objHttpResponse = objHttpRequest. GetResponse, an
    exception is thrown stating the request timed out.
    >
    pointers appreciated..
    >
    thanks in advance

    Comment

    • Mike McIntyre

      #3
      Re: consume web service via httpwebrequest

      You may find the information at the link below userful:



      Mike McIntytre [MVP]
      www.getdotnetcode.com

      "hharry" <paulquigley@ny c.comwrote in message
      news:1152813893 .756737.210460@ m73g2000cwd.goo glegroups.com.. .
      hello all,
      >
      trying to consume a simple web service using httpwebrequest instead of
      generating a proxy class.
      >
      code for simple web service:
      >
      Imports System.Web.Serv ices
      >
      <System.Web.Ser vices.WebServic e(Namespace :=
      "http://tempuri.org/SoapServer/Service1")_
      Public Class Service1
      Inherits System.Web.Serv ices.WebService
      >
      <WebMethod()_
      Public Function HelloWorld() As String
      Return "Hello World"
      End Function
      End Class
      >
      code to consume web service using httpwebrequest object:
      >
      Dim sTmp As String
      sTmp = "" & _
      "<?xml version=" + Chr(34) + "1.0" + Chr(34) + " encoding="
      + Chr(34) + "utf-8" + Chr(34) + "?>" & _
      "<soap:Enve lope xmlns:xsi=" + Chr(34) +
      "http://www.w3.org/2001/XMLSchema-instance" + Chr(34) + " xmlns:xsd=" +
      Chr(34) + "http://www.w3.org/2001/XMLSchema" + Chr(34) + " xmlns:soap="
      + Chr(34) + "http://schemas.xmlsoap .org/soap/envelope/" + Chr(34) + ">"
      & _
      "<soap:Body >" & _
      "<HelloWorl d xmlns=" + Chr(34) +
      "http://tempuri.org/SoapServer/Service1" + Chr(34) + " />" & _
      "</soap:Body>" & _
      "</soap:Envelope>"
      >
      objHttpRequest =
      WebRequest.Crea te("http://localhost/SoapServer/Service1.asmx")
      objHttpRequest. Method = "POST"
      objHttpRequest. ContentType = "text/xml"
      objHttpRequest. ContentLength = sTmp.Length
      objHttpRequest. Headers.Add("SO APAction",
      "http://tempuri.org/SoapServer/Service1/HelloWorld")
      >
      myWriter = New StreamWriter(ob jHttpRequest.Ge tRequestStream( ))
      myWriter.Write( sTmp)
      >
      objHttpResponse = objHttpRequest. GetResponse
      objResponseStre am = New
      StreamReader(ob jHttpResponse.G etResponseStrea m())
      sResult = objResponseStre am.ReadToEnd
      objResponseStre am.Close()
      >
      =============== =============== =============== =============== =====
      =============== =============== =============== =============== =====
      >
      the code fails at objHttpResponse = objHttpRequest. GetResponse, an
      exception is thrown stating the request timed out.
      >
      pointers appreciated..
      >
      thanks in advance
      >

      Comment

      Working...