473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.WebException 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.Create(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 = "application/x-www-form-urlencoded"
28
29 End If
30
31 ' Set Content-Type
32
33 If (ContentType <"") Then
34
35 Request.ContentType = ContentType
36
37 Request.ContentLength = PostData.Length
38
39 End If
40
41 'Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(PostData)
42
43 ' Send Request, If Request
44
45 If (Method = HTTPMethod.HTTP_POST) Then
46
47 Try
48
49 SW = New StreamWriter(Request.GetRequestStream())
50
51 SW.Write("XML=" & RepalceData(PostData))
52
53 Catch Ex As Exception
54
55 Throw Ex
56
57 MsgBox(Ex.Message)
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.GetResponse()
74
75 SR = New StreamReader(Response.GetResponseStream())
76
77 ResponseData = SR.ReadToEnd()
78
79 MsgBox(ResponseData)
80
81 Catch Wex As System.Net.WebException
82
83 SR = New StreamReader(Wex.Response.GetResponseStream())
84
85 ResponseData = SR.ReadToEnd()
86
87 Throw New Exception(ResponseData)
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(ByRef 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

Mar 26 '08 #1
2 5285
Set ContentLength to correct value....
Your code
>37: Request.ContentLength = PostData.Length
.....
49 SW = New StreamWriter(Request.GetRequestStream())
50
51 SW.Write("XML=" & RepalceData(PostData))
adding "XML=" increases the length by 4 characters.... Not sure what
ReplaceData does... So it can be even more...

George.

"pelegk1" <pe*****@discussions.microsoft.comwrote in message
news:12**********************************@microsof t.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.WebException 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.Create(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 = "application/x-www-form-urlencoded"
28
29 End If
30
31 ' Set Content-Type
32
33 If (ContentType <"") Then
34
35 Request.ContentType = ContentType
36
37 Request.ContentLength = PostData.Length
38
39 End If
40
41 'Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(PostData)
42
43 ' Send Request, If Request
44
45 If (Method = HTTPMethod.HTTP_POST) Then
46
47 Try
48
49 SW = New StreamWriter(Request.GetRequestStream())
50
51 SW.Write("XML=" & RepalceData(PostData))
52
53 Catch Ex As Exception
54
55 Throw Ex
56
57 MsgBox(Ex.Message)
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.GetResponse()
74
75 SR = New StreamReader(Response.GetResponseStream())
76
77 ResponseData = SR.ReadToEnd()
78
79 MsgBox(ResponseData)
80
81 Catch Wex As System.Net.WebException
82
83 SR = New StreamReader(Wex.Response.GetResponseStream())
84
85 ResponseData = SR.ReadToEnd()
86
87 Throw New Exception(ResponseData)
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(ByRef 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

Mar 27 '08 #2
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.ContentLength = PostData.Length
.....
49 SW = New StreamWriter(Request.GetRequestStream())
50
51 SW.Write("XML=" & RepalceData(PostData))

adding "XML=" increases the length by 4 characters.... Not sure what
ReplaceData does... So it can be even more...

George.

"pelegk1" <pe*****@discussions.microsoft.comwrote in message
news:12**********************************@microsof t.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.WebException 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.Create(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 = "application/x-www-form-urlencoded"
28
29 End If
30
31 ' Set Content-Type
32
33 If (ContentType <"") Then
34
35 Request.ContentType = ContentType
36
37 Request.ContentLength = PostData.Length
38
39 End If
40
41 'Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(PostData)
42
43 ' Send Request, If Request
44
45 If (Method = HTTPMethod.HTTP_POST) Then
46
47 Try
48
49 SW = New StreamWriter(Request.GetRequestStream())
50
51 SW.Write("XML=" & RepalceData(PostData))
52
53 Catch Ex As Exception
54
55 Throw Ex
56
57 MsgBox(Ex.Message)
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.GetResponse()
74
75 SR = New StreamReader(Response.GetResponseStream())
76
77 ResponseData = SR.ReadToEnd()
78
79 MsgBox(ResponseData)
80
81 Catch Wex As System.Net.WebException
82
83 SR = New StreamReader(Wex.Response.GetResponseStream())
84
85 ResponseData = SR.ReadToEnd()
86
87 Throw New Exception(ResponseData)
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(ByRef 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


Mar 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KS | last post by:
Is it possible to write a javascript that makes a popup window when someone click on buttons/href on my page and close itself when the new page is about to get loaded? I want to prevent the user...
2
by: andrewkooi | last post by:
Greetings, I do not know whether such code exist but no harm asking: Is it possible for me to make visible a close button if the active window is a popup window or a new window (other than the...
6
by: andrewkooi | last post by:
Greetings, I do not know whether such code exist but no harm asking: Is it possible for me to make visible a close button if the active window is a popup window or a new window (other than the...
13
by: lkrubner | last post by:
Suppose I need to get an image as a stream of bytes. I want to store this in a variable and then embed it in some Postscript code. In my Postscript code, the image might look like this: {<...
1
by: Al Cadalzo | last post by:
I'm getting this error In DEBUG mode with 'Break into the debugger' option turned on I get an IO Exception: 'Cannot close stream until all bytes are written.' Followed by a 'WebException': The...
0
by: Tom wilson | last post by:
Yes, I'm absolutely serious. I'm in the VS.Net editor with an HTML page loaded. All the links are broken. So I right click an image and choose Properties. The window appears. No matter...
7
by: tapanreddy | last post by:
I am looking to perform an action when we close the window using the close tab at the top of the screen. I know how to do it using a close button but I was wondering if there is way to achieve the...
6
by: tabakaka | last post by:
what is the condition if i want to pop a new window once i close the current one? is it..? if window.close { window.open("abc.aspx", "abc",...
9
by: coconet | last post by:
I am trying to use a StreamReader to read consecutive bytes into a byte array until a ";" is hit, then store everything read up until that point into a new byte array. I have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.