window application-WebRequest -Cannot close stream until all bytes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cGVsZWdrMQ==?=

    window application-WebRequest -Cannot close stream until all bytes

    when i send an xml to a server using WebRequest object (i am sending a
    paramater+xml in size of about 250 chars)

    i recve an error :

    System.Net.WebE xception was unhandled
    Message="The request was aborted: The request was canceled."

    and the inner exception is :{"Cannot close stream until all bytes are
    written."}

    this happens row 63 ==>>SW.Close()


    as i understand this , the request to the server didnt finish the sending
    and its being closed!

    how can i prevent this?

    1 Public Shared Function Send(ByVal URL As String, _
    2
    3 Optional ByVal PostData As String = "", _
    4
    5 Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP _GET, _
    6
    7 Optional ByVal ContentType As String = "")
    8
    9 Dim Request As HttpWebRequest = WebRequest.Crea te(URL)
    10
    11 Dim Response As HttpWebResponse
    12
    13 Dim SW As StreamWriter
    14
    15 Dim SR As StreamReader
    16
    17 Dim ResponseData As String
    18
    19 ' Prepare Request Object
    20
    21 Request.Method = Method.ToString ().Substring(5)
    22
    23 ' Set form/post content-type if necessary
    24
    25 If (Method = HTTPMethod.HTTP _POST AndAlso PostData <"" AndAlso
    ContentType = "") Then
    26
    27 ContentType = "applicatio n/x-www-form-urlencoded"
    28
    29 End If
    30
    31 ' Set Content-Type
    32
    33 If (ContentType <"") Then
    34
    35 Request.Content Type = ContentType
    36
    37 Request.Content Length = PostData.Length
    38
    39 End If
    40
    41 'Dim bytes As Byte() = System.Text.Enc oding.UTF8.GetB ytes(PostData)
    42
    43 ' Send Request, If Request
    44
    45 If (Method = HTTPMethod.HTTP _POST) Then
    46
    47 Try
    48
    49 SW = New StreamWriter(Re quest.GetReques tStream())
    50
    51 SW.Write("XML=" & RepalceData(Pos tData))
    52
    53 Catch Ex As Exception
    54
    55 Throw Ex
    56
    57 MsgBox(Ex.Messa ge)
    58
    59 '
    60
    61 Finally
    62
    63 SW.Close()
    64
    65 End Try
    66
    67 End If
    68
    69 ' Receive Response
    70
    71 Try
    72
    73 Response = Request.GetResp onse()
    74
    75 SR = New StreamReader(Re sponse.GetRespo nseStream())
    76
    77 ResponseData = SR.ReadToEnd()
    78
    79 MsgBox(Response Data)
    80
    81 Catch Wex As System.Net.WebE xception
    82
    83 SR = New StreamReader(We x.Response.GetR esponseStream() )
    84
    85 ResponseData = SR.ReadToEnd()
    86
    87 Throw New Exception(Respo nseData)
    88
    89 Finally
    90
    91 SR.Close()
    92
    93 End Try
    94
    95 Return ResponseData
    96
    97 End Function
    98
    99
    100 Public Shared Function RepalceData(ByR ef data)
    101
    102 data = Replace(data, "%", "%25")
    103
    104 data = Replace(data, " ", "%20")
    105
    106 data = Replace(data, "#", "%23")
    107
    108 data = Replace(data, "&", "%26")
    109
    110 data = Replace(data, "?", "%3F")
    111
    112 data = Replace(data, "+", "%2B")
    113
    114 RepalceData = data
    115
    116 End Function
    117


    thnaks i nadvance

    peleg

  • George Ter-Saakov

    #2
    Re: window application-WebRequest -Cannot close stream until all bytes

    Set ContentLength to correct value....
    Your code
    >37: Request.Content Length = PostData.Length
    .....
    49 SW = New StreamWriter(Re quest.GetReques tStream())
    50
    51 SW.Write("XML=" & RepalceData(Pos tData))
    adding "XML=" increases the length by 4 characters.... Not sure what
    ReplaceData does... So it can be even more...

    George.



    "pelegk1" <pelegk1@discus sions.microsoft .comwrote in message
    news:12150602-25ED-4780-A4B3-702F334CFA41@mi crosoft.com...
    when i send an xml to a server using WebRequest object (i am sending a
    paramater+xml in size of about 250 chars)
    >
    i recve an error :
    >
    System.Net.WebE xception was unhandled
    Message="The request was aborted: The request was canceled."
    >
    and the inner exception is :{"Cannot close stream until all bytes are
    written."}
    >
    this happens row 63 ==>>SW.Close()
    >
    >
    as i understand this , the request to the server didnt finish the sending
    and its being closed!
    >
    how can i prevent this?
    >
    1 Public Shared Function Send(ByVal URL As String, _
    2
    3 Optional ByVal PostData As String = "", _
    4
    5 Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP _GET, _
    6
    7 Optional ByVal ContentType As String = "")
    8
    9 Dim Request As HttpWebRequest = WebRequest.Crea te(URL)
    10
    11 Dim Response As HttpWebResponse
    12
    13 Dim SW As StreamWriter
    14
    15 Dim SR As StreamReader
    16
    17 Dim ResponseData As String
    18
    19 ' Prepare Request Object
    20
    21 Request.Method = Method.ToString ().Substring(5)
    22
    23 ' Set form/post content-type if necessary
    24
    25 If (Method = HTTPMethod.HTTP _POST AndAlso PostData <"" AndAlso
    ContentType = "") Then
    26
    27 ContentType = "applicatio n/x-www-form-urlencoded"
    28
    29 End If
    30
    31 ' Set Content-Type
    32
    33 If (ContentType <"") Then
    34
    35 Request.Content Type = ContentType
    36
    37 Request.Content Length = PostData.Length
    38
    39 End If
    40
    41 'Dim bytes As Byte() = System.Text.Enc oding.UTF8.GetB ytes(PostData)
    42
    43 ' Send Request, If Request
    44
    45 If (Method = HTTPMethod.HTTP _POST) Then
    46
    47 Try
    48
    49 SW = New StreamWriter(Re quest.GetReques tStream())
    50
    51 SW.Write("XML=" & RepalceData(Pos tData))
    52
    53 Catch Ex As Exception
    54
    55 Throw Ex
    56
    57 MsgBox(Ex.Messa ge)
    58
    59 '
    60
    61 Finally
    62
    63 SW.Close()
    64
    65 End Try
    66
    67 End If
    68
    69 ' Receive Response
    70
    71 Try
    72
    73 Response = Request.GetResp onse()
    74
    75 SR = New StreamReader(Re sponse.GetRespo nseStream())
    76
    77 ResponseData = SR.ReadToEnd()
    78
    79 MsgBox(Response Data)
    80
    81 Catch Wex As System.Net.WebE xception
    82
    83 SR = New StreamReader(We x.Response.GetR esponseStream() )
    84
    85 ResponseData = SR.ReadToEnd()
    86
    87 Throw New Exception(Respo nseData)
    88
    89 Finally
    90
    91 SR.Close()
    92
    93 End Try
    94
    95 Return ResponseData
    96
    97 End Function
    98
    99
    100 Public Shared Function RepalceData(ByR ef data)
    101
    102 data = Replace(data, "%", "%25")
    103
    104 data = Replace(data, " ", "%20")
    105
    106 data = Replace(data, "#", "%23")
    107
    108 data = Replace(data, "&", "%26")
    109
    110 data = Replace(data, "?", "%3F")
    111
    112 data = Replace(data, "+", "%2B")
    113
    114 RepalceData = data
    115
    116 End Function
    117
    >
    >
    thnaks i nadvance
    >
    peleg
    >

    Comment

    • =?Utf-8?B?cGVsZWdrMQ==?=

      #3
      Re: window application-WebRequest -Cannot close stream until all b

      solved!
      before line 63 SW.Close()
      there should be SW.Flush()
      and i fixed the content length thanks:)


      "George Ter-Saakov" wrote:
      Set ContentLength to correct value....
      Your code
      37: Request.Content Length = PostData.Length
      .....
      49 SW = New StreamWriter(Re quest.GetReques tStream())
      50
      51 SW.Write("XML=" & RepalceData(Pos tData))
      >
      adding "XML=" increases the length by 4 characters.... Not sure what
      ReplaceData does... So it can be even more...
      >
      George.
      >
      >
      >
      "pelegk1" <pelegk1@discus sions.microsoft .comwrote in message
      news:12150602-25ED-4780-A4B3-702F334CFA41@mi crosoft.com...
      when i send an xml to a server using WebRequest object (i am sending a
      paramater+xml in size of about 250 chars)

      i recve an error :

      System.Net.WebE xception was unhandled
      Message="The request was aborted: The request was canceled."

      and the inner exception is :{"Cannot close stream until all bytes are
      written."}

      this happens row 63 ==>>SW.Close()


      as i understand this , the request to the server didnt finish the sending
      and its being closed!

      how can i prevent this?

      1 Public Shared Function Send(ByVal URL As String, _
      2
      3 Optional ByVal PostData As String = "", _
      4
      5 Optional ByVal Method As HTTPMethod = HTTPMethod.HTTP _GET, _
      6
      7 Optional ByVal ContentType As String = "")
      8
      9 Dim Request As HttpWebRequest = WebRequest.Crea te(URL)
      10
      11 Dim Response As HttpWebResponse
      12
      13 Dim SW As StreamWriter
      14
      15 Dim SR As StreamReader
      16
      17 Dim ResponseData As String
      18
      19 ' Prepare Request Object
      20
      21 Request.Method = Method.ToString ().Substring(5)
      22
      23 ' Set form/post content-type if necessary
      24
      25 If (Method = HTTPMethod.HTTP _POST AndAlso PostData <"" AndAlso
      ContentType = "") Then
      26
      27 ContentType = "applicatio n/x-www-form-urlencoded"
      28
      29 End If
      30
      31 ' Set Content-Type
      32
      33 If (ContentType <"") Then
      34
      35 Request.Content Type = ContentType
      36
      37 Request.Content Length = PostData.Length
      38
      39 End If
      40
      41 'Dim bytes As Byte() = System.Text.Enc oding.UTF8.GetB ytes(PostData)
      42
      43 ' Send Request, If Request
      44
      45 If (Method = HTTPMethod.HTTP _POST) Then
      46
      47 Try
      48
      49 SW = New StreamWriter(Re quest.GetReques tStream())
      50
      51 SW.Write("XML=" & RepalceData(Pos tData))
      52
      53 Catch Ex As Exception
      54
      55 Throw Ex
      56
      57 MsgBox(Ex.Messa ge)
      58
      59 '
      60
      61 Finally
      62
      63 SW.Close()
      64
      65 End Try
      66
      67 End If
      68
      69 ' Receive Response
      70
      71 Try
      72
      73 Response = Request.GetResp onse()
      74
      75 SR = New StreamReader(Re sponse.GetRespo nseStream())
      76
      77 ResponseData = SR.ReadToEnd()
      78
      79 MsgBox(Response Data)
      80
      81 Catch Wex As System.Net.WebE xception
      82
      83 SR = New StreamReader(We x.Response.GetR esponseStream() )
      84
      85 ResponseData = SR.ReadToEnd()
      86
      87 Throw New Exception(Respo nseData)
      88
      89 Finally
      90
      91 SR.Close()
      92
      93 End Try
      94
      95 Return ResponseData
      96
      97 End Function
      98
      99
      100 Public Shared Function RepalceData(ByR ef data)
      101
      102 data = Replace(data, "%", "%25")
      103
      104 data = Replace(data, " ", "%20")
      105
      106 data = Replace(data, "#", "%23")
      107
      108 data = Replace(data, "&", "%26")
      109
      110 data = Replace(data, "?", "%3F")
      111
      112 data = Replace(data, "+", "%2B")
      113
      114 RepalceData = data
      115
      116 End Function
      117


      thnaks i nadvance

      peleg
      >
      >
      >

      Comment

      Working...