Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. In .Net how can I get a response to WebClient.Open write instead of Stream was not readable

In .Net how can I get a response to WebClient.Open write instead of Stream was not readable

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestion
19 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q QuickBooksDev

    It doesn't compile. Please send c# code. GetResponse is not a member of io stream and rs not defined. How should MyDataStream and rs be defined? I am not sure of the response. The doc that I have on this service is very limited and not written for .Net.

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #10

    http://stackoverflow.com/questions/8222092/sending-http-post-with-system-net-webclient[^]

    A guide to posting questions on CodeProject

    Click this: Asking questions is a skill. Seriously, do it.
    Dave Kreskowiak

    1 Reply Last reply
    0
    • Q QuickBooksDev

      I understand that I have been coding for many years but I have never used WebClient before and do not know what to look for. I was expecting that is there is some setting or parameter that needs to be set to get the response. Do you have any explicit things to look for? Or do you have any code that does something similar that you can provide?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #11

      QuickBooksDev wrote:

      I have been coding for many years

      Then you should understand how to debug your code.

      QuickBooksDev wrote:

      I have never used WebClient before

      Then the documentation is the first place to look, see http://msdn.microsoft.com/en-us/library/a3xa6ys0(v=vs.110).aspx[^].

      Q 1 Reply Last reply
      0
      • L Lost User

        QuickBooksDev wrote:

        I have been coding for many years

        Then you should understand how to debug your code.

        QuickBooksDev wrote:

        I have never used WebClient before

        Then the documentation is the first place to look, see http://msdn.microsoft.com/en-us/library/a3xa6ys0(v=vs.110).aspx[^].

        Q Offline
        Q Offline
        QuickBooksDev
        wrote on last edited by
        #12

        The doc is where I started and copied the code from their examples. I just does not show getting a response from OpenWrite. There is very little of my code to actually debug. I am not sure if I am sending the wrong data to the service or that the WebClient is coded wrong.

        L 1 Reply Last reply
        0
        • Q QuickBooksDev

          The doc is where I started and copied the code from their examples. I just does not show getting a response from OpenWrite. There is very little of my code to actually debug. I am not sure if I am sending the wrong data to the service or that the WebClient is coded wrong.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #13

          QuickBooksDev wrote:

          I am not sure if I am sending the wrong data to the service

          And all i can say in response, once again, is that you need to do some debugging to find out. There is no detail in your question that can help anyone here to guess what might be going wrong.

          1 Reply Last reply
          0
          • Q QuickBooksDev

            It also happens if I do not close it.

            If postData = NIL Then
            MydataStream = MyWebClient.OpenRead(address:=MySend2URL)
            Else
            'MydataStream
            MydataStream = MyWebClient.OpenWrite(address:=MySend2URL, method:="POST")
            MydataStream.Write(postArray, 0, postArray.Length)
            'MydataStream.Close() ' "The remote server returned an error: (401) Unauthorized."
            End If
            'MySend2URL &= ":443"
            Dim ThisResponse As String = NIL ' MyWebClient.Encoding.GetString(MydataStream)
            Dim reader As New StreamReader(MydataStream) ' <=== Dies here with "Stream was not readable."

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #14

            If you don't close it the stream will still contain the data that is in the postarray, with the "current position" at the end of the stream. If you want to read it, you'd need to reset the position. Then again, why would you read from the stream the data that you just wrote to it?

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            Q 1 Reply Last reply
            0
            • L Lost User

              If you don't close it the stream will still contain the data that is in the postarray, with the "current position" at the end of the stream. If you want to read it, you'd need to reset the position. Then again, why would you read from the stream the data that you just wrote to it?

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              Q Offline
              Q Offline
              QuickBooksDev
              wrote on last edited by
              #15

              I don't want to read from what I wrote. The data I wrote in not in the stream but in postArray. MydataStream.Write(postArray, 0, postArray.Length) From the MS example Steam = MyWebClient.OpenWrite(... It was my assumption that this was be a response like it is if it were an OpenRead (which does work). The MSDN sample doesn't show it processing a response.

              L 1 Reply Last reply
              0
              • Q QuickBooksDev

                I don't want to read from what I wrote. The data I wrote in not in the stream but in postArray. MydataStream.Write(postArray, 0, postArray.Length) From the MS example Steam = MyWebClient.OpenWrite(... It was my assumption that this was be a response like it is if it were an OpenRead (which does work). The MSDN sample doesn't show it processing a response.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #16

                See http://msdn.microsoft.com/en-us/library/781fwaz8(v=vs.110).aspx[^].

                Q 1 Reply Last reply
                0
                • L Lost User

                  See http://msdn.microsoft.com/en-us/library/781fwaz8(v=vs.110).aspx[^].

                  Q Offline
                  Q Offline
                  QuickBooksDev
                  wrote on last edited by
                  #17

                  So what are you saying by just putting in that link? That is the sample we used to do OpenReads which works when there is no postdata. Do I need to do an OpenRead after the OpenWrite? If so should the same Steam be used or another? If the same should the stream be closed after the write and before the read?

                  L 1 Reply Last reply
                  0
                  • Q QuickBooksDev

                    So what are you saying by just putting in that link? That is the sample we used to do OpenReads which works when there is no postdata. Do I need to do an OpenRead after the OpenWrite? If so should the same Steam be used or another? If the same should the stream be closed after the write and before the read?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #18

                    I have not tried this myself but that is what I assume. Maybe the HttpWebRequest Class[^] would suit your purpose better.

                    1 Reply Last reply
                    0
                    • Q QuickBooksDev

                      I am using VB.2013. Code :

                      Dim MydataStream As Stream = Nothing
                      MydataStream = MyWebClient.OpenWrite(address:=MySend2URL, method:="POST")
                      Dim postArray As Byte() = Encoding.ASCII.GetBytes(postData)
                      MydataStream.Write(postArray, 0, postArray.Length)
                      MydataStream.Close()
                      Dim reader As New StreamReader(MydataStream) ' <=== Dies here with "Stream was not readable."

                      How can I get a response that can be used and not the Stream is not readable error.

                      Q Offline
                      Q Offline
                      QuickBooksDev
                      wrote on last edited by
                      #19

                      Found what I had to do. I had to get the target server people to trace what they were getting and tell me what was wrong. Eventually found out that I had to add the below ContentType and remove the leading "?" from the postData. None of the debugging in the .net code was of any help since without the below the postdata was being sent as 1 field and they were not returning anything if the postdata was invalid. MyWebRequest.ContentType ="application/x-wwww.form-urlencoded"

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups