window application-WebRequest -Cannot close stream until all bytes

P

pelegk1

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
 
G

George Ter-Saakov

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.
 
P

pelegk1

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top