HttpWebRequest from winform

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

    #1

    HttpWebRequest from winform

    I have code which works as an asp.net page that posts an
    xml file to web page and gets a response back.

    When the the calls GetResponse() it goes into the page
    it's posting to to and works fine. When it's been ported
    to a winform it doesn't work on the GetResponse() call.

    I think it probably needs credentials but not sure what
    to use, tried a few ids w/o any luck.

    The asp.net page as well as the winform are on the same
    server for now but will later be on seperate servers.

    Thanks,

    Brian
  • Jon Skeet [C# MVP]

    #2
    Re: HttpWebRequest from winform

    Brian Brown <bbrown@hotmail .com> wrote:[color=blue]
    > I have code which works as an asp.net page that posts an
    > xml file to web page and gets a response back.
    >
    > When the the calls GetResponse() it goes into the page
    > it's posting to to and works fine. When it's been ported
    > to a winform it doesn't work on the GetResponse() call.
    >
    > I think it probably needs credentials but not sure what
    > to use, tried a few ids w/o any luck.
    >
    > The asp.net page as well as the winform are on the same
    > server for now but will later be on seperate servers.[/color]

    Could you post a short but complete program which demonstrates the
    problem?

    See http://www.pobox.com/~skeet/csharp/complete.html for details of
    what I mean by that.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Guest's Avatar

      #3
      Re: HttpWebRequest from winform

      Web page code that works:

      Imports System.IO
      Imports System.Xml
      Imports System.Xml.Sche ma
      Imports System.Net.Cred entialCache
      Imports System.Text
      Imports System.net

      Public Class Class1
      Private Sub SendDoc(ByRef sData As String)
      Const k_send_to_url As String
      = "http://localhost/Reader/Receive.aspx"

      Dim request_obj As HttpWebRequest
      Dim request_stream As Stream
      Dim response_obj As HttpWebResponse
      Dim xml_reader As XmlTextReader
      Dim docobj As New XmlDocument


      Try
      request_obj = CType(WebReques t.Create
      (k_send_to_url) , HttpWebRequest)

      request_obj.Met hod = "POST"
      request_obj.Con tentType = "text/xml"
      request_obj.Tim eout = 999999999
      request_obj.All owWriteStreamBu ffering = True

      request_stream = request_obj.Get RequestStream
      ()

      docobj.LoadXml( sData)

      docobj.Save(req uest_stream)

      request_stream. Close()
      docobj = Nothing

      response_obj = request_obj.Get Response()

      xml_reader = New XmlTextReader
      (response_obj.G etResponseStrea m())
      docobj = New XmlDocument
      docobj.Load(xml _reader)

      request_obj = Nothing
      request_stream = Nothing
      response_obj = Nothing

      Catch ex As Exception
      Exit Sub
      End Try

      End Sub


      End Class

      Windows code that doesn't:


      Imports System.IO
      Imports System.Xml
      Imports System.Xml.Sche ma
      Imports System.Net.Cred entialCache
      Imports System.Text
      Imports System.net

      Public Class Class1

      Private Sub SendDoc(ByRef sData As String)
      Const k_send_to_url As String
      = "http://localhost/Reader/Receive.aspx"

      Dim request_obj As HttpWebRequest
      Dim request_stream As Stream
      Dim response_obj As HttpWebResponse
      Dim xml_reader As XmlTextReader
      Dim docobj As New XmlDocument


      Try


      request_obj = CType(WebReques t.Create
      (k_send_to_url) , HttpWebRequest)

      request_obj.Met hod = "POST"
      request_obj.Con tentType = "text/xml"
      request_obj.Tim eout = 999999999
      request_obj.All owWriteStreamBu ffering = True

      request_stream = request_obj.Get RequestStream
      ()

      docobj.LoadXml( sData)

      docobj.Save(req uest_stream)

      request_stream. Close()
      docobj = Nothing

      response_obj = request_obj.Get Response()

      xml_reader = New XmlTextReader
      (response_obj.G etResponseStrea m())
      docobj = New XmlDocument
      docobj.Load(xml _reader)

      request_obj = Nothing
      request_stream = Nothing
      response_obj = Nothing

      Catch ex As Exception
      Exit Sub
      End Try
      End Sub
      End Class



      [color=blue]
      >-----Original Message-----
      >Brian Brown <bbrown@hotmail .com> wrote:[color=green]
      >> I have code which works as an asp.net page that posts[/color][/color]
      an[color=blue][color=green]
      >> xml file to web page and gets a response back.
      >>
      >> When the the calls GetResponse() it goes into the page
      >> it's posting to to and works fine. When it's been[/color][/color]
      ported[color=blue][color=green]
      >> to a winform it doesn't work on the GetResponse() call.
      >>
      >> I think it probably needs credentials but not sure[/color][/color]
      what[color=blue][color=green]
      >> to use, tried a few ids w/o any luck.
      >>
      >> The asp.net page as well as the winform are on the[/color][/color]
      same[color=blue][color=green]
      >> server for now but will later be on seperate servers.[/color]
      >
      >Could you post a short but complete program which[/color]
      demonstrates the[color=blue]
      >problem?
      >
      >See http://www.pobox.com/~skeet/csharp/complete.html for[/color]
      details of[color=blue]
      >what I mean by that.
      >
      >--
      >Jon Skeet - <skeet@pobox.co m>
      >http://www.pobox.com/~skeet
      >If replying to the group, please do not mail me too
      >.
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: HttpWebRequest from winform

        <anonymous@disc ussions.microso ft.com> wrote:

        <snip>
        [color=blue]
        > Windows code that doesn't:[/color]

        And what happens, exactly? I notice that when you catch an exception,
        you don't do anything with it - is an exception being thrown? If so,
        what's the contents of the exception?

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Guest's Avatar

          #5
          Re: HttpWebRequest from winform


          When the response_obj = request_obj.Get Response()

          code executes if you put a breakpoint on it, it should
          post the xml file to the page defined in k_send_to_url.

          This happens in the web version but not in the windows
          version which I intend to turn into a windows service.

          No error is thrown until I try to use the result which
          should be an xml document.

          It has to be security somehow I guess, but can't seem to
          figure out what. I tried setting credentials and it still
          didn't work.

          Thanks!
          [color=blue]
          >-----Original Message-----
          > <anonymous@disc ussions.microso ft.com> wrote:
          >
          ><snip>
          >[color=green]
          >> Windows code that doesn't:[/color]
          >
          >And what happens, exactly? I notice that when you catch[/color]
          an exception,[color=blue]
          >you don't do anything with it - is an exception being[/color]
          thrown? If so,[color=blue]
          >what's the contents of the exception?
          >
          >--
          >Jon Skeet - <skeet@pobox.co m>
          >http://www.pobox.com/~skeet
          >If replying to the group, please do not mail me too
          >.
          >[/color]

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: HttpWebRequest from winform

            <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
            > When the response_obj = request_obj.Get Response()
            >
            > code executes if you put a breakpoint on it, it should
            > post the xml file to the page defined in k_send_to_url.
            >
            > This happens in the web version but not in the windows
            > version which I intend to turn into a windows service.
            >
            > No error is thrown until I try to use the result which
            > should be an xml document.
            >
            > It has to be security somehow I guess, but can't seem to
            > figure out what. I tried setting credentials and it still
            > didn't work.[/color]

            You haven't said what *does* happen in the Windows version though. What
            happens you you execute request_obj.Get Response()? What does it return?
            Does it block? Does it throw an exception?

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Guest's Avatar

              #7
              Re: HttpWebRequest from winform

              Nothing happens, no error, it doesn't go into the other
              page, the error is on the next line when it expects an
              xml document and there's nothing.

              [color=blue]
              >-----Original Message-----
              > <anonymous@disc ussions.microso ft.com> wrote:[color=green]
              >> When the response_obj = request_obj.Get Response()
              >>
              >> code executes if you put a breakpoint on it, it should
              >> post the xml file to the page defined in k_send_to_url.
              >>
              >> This happens in the web version but not in the windows
              >> version which I intend to turn into a windows service.
              >>
              >> No error is thrown until I try to use the result which
              >> should be an xml document.
              >>
              >> It has to be security somehow I guess, but can't seem[/color][/color]
              to[color=blue][color=green]
              >> figure out what. I tried setting credentials and it[/color][/color]
              still[color=blue][color=green]
              >> didn't work.[/color]
              >
              >You haven't said what *does* happen in the Windows[/color]
              version though. What[color=blue]
              >happens you you execute request_obj.Get Response()? What[/color]
              does it return?[color=blue]
              >Does it block? Does it throw an exception?
              >
              >--
              >Jon Skeet - <skeet@pobox.co m>
              >http://www.pobox.com/~skeet
              >If replying to the group, please do not mail me too
              >.
              >[/color]

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: HttpWebRequest from winform

                <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
                > Nothing happens, no error, it doesn't go into the other
                > page, the error is on the next line when it expects an
                > xml document and there's nothing.[/color]

                I suggest you look at the contents of the stream then, and find out
                what's in there. Also look at your webserver logs to find out whether
                it thinks it sent any content back - and preferably what it thought it
                received, too.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Guest's Avatar

                  #9
                  Re: HttpWebRequest from winform

                  It doesn't even go into the page. I have a breakpoint
                  where in the asp page it goes into the calling page, the
                  windows one doesn't even go into the page.

                  [color=blue]
                  >-----Original Message-----
                  > <anonymous@disc ussions.microso ft.com> wrote:[color=green]
                  >> Nothing happens, no error, it doesn't go into the[/color][/color]
                  other[color=blue][color=green]
                  >> page, the error is on the next line when it expects an
                  >> xml document and there's nothing.[/color]
                  >
                  >I suggest you look at the contents of the stream then,[/color]
                  and find out[color=blue]
                  >what's in there. Also look at your webserver logs to[/color]
                  find out whether[color=blue]
                  >it thinks it sent any content back - and preferably what[/color]
                  it thought it[color=blue]
                  >received, too.
                  >
                  >--
                  >Jon Skeet - <skeet@pobox.co m>
                  >http://www.pobox.com/~skeet
                  >If replying to the group, please do not mail me too
                  >.
                  >[/color]

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: HttpWebRequest from winform

                    <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
                    > It doesn't even go into the page. I have a breakpoint
                    > where in the asp page it goes into the calling page, the
                    > windows one doesn't even go into the page.[/color]

                    And yet GetResponse returns with no exception? What status does the
                    returned response have?

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: HttpWebRequest from winform

                      <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
                      > HttpWebResponse .statusmessage = ok
                      >
                      > same for HttpWebResponse .statuscode
                      >
                      > yet it never went into the page, nor does it contain the
                      > returned content.[/color]

                      In that case, I suggest you find what it *did* go to. Does it contain
                      *any* content? My guess is that perhaps a proxy is getting in the way
                      unexpectedly.

                      Try using a network sniffer to find out what's going on.

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      Working...