Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

window application-WebRequest -Cannot close stream until all bytes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pelegk1 - 26 Mar 2008 23:16 GMT
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
George Ter-Saakov - 27 Mar 2008 18:16 GMT
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.

> when i send an xml to a server using WebRequest object (i am sending a
> paramater+xml in size of about 250 chars)
[quoted text clipped - 136 lines]
>
> peleg
pelegk1 - 27 Mar 2008 19:08 GMT
solved!
before line 63 SW.Close()
there should be SW.Flush()
and i fixed the content length thanks:)

> Set ContentLength to correct value....
> Your code
[quoted text clipped - 149 lines]
> >
> > peleg

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.