468,761 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,761 developers. It's quick & easy.

HTTPWebRequest error | Transport connection related

I am working on a VB .net application which accesses a Java Servlet.
This servlet returns a binary stream that is written onto a PDF and then
this file is printed. Even though this file is downloaded and printed on most
environments, it fails to do so on one of the environments with the following
error:
"Unable to read data from the transport connection."

Also to be noted is the fact that when this servlet is hit from a different
geography, the response is obtained in perfect order and the file also gets
generated. Here's the sample code that does it:

Dim req As HttpWebRequest
Dim res As HttpWebResponse
req = WebRequest.Create("https://**********")
req.Method = "GET"
req.CookieContainer = New CookieContainer
req.CookieContainer.Add(ccContainer.GetCookies(req .RequestUri))
req.ContentType = "text/xml"
' Send the text to the webserver
res = req.GetResponse()
Dim Cook As Cookie
For Each Cook In res.Cookies
Me.ccContainer.Add(Cook)
'MsgBox(Cook.Name)
Next
' Wrap the response stream with a text-based reader
Dim sr As BinaryReader = New BinaryReader(res.GetResponseStream())
' Read the returned text into a textbox
Dim bWriter As New BinaryWriter(File.OpenWrite("temp.pdf"))
bWriter.Write(sr.ReadBytes(99999999))
bWriter.Close()

It seems to me that the problem creeps up when you're trying to access the
response stream to associate with the binary reader. However, I have not been
able to validate this.

Any pointers to this are gladly appreciated.

Regards,
Sanjeev

Jan 24 '06 #1
3 1788
Hello Sanjeev,
I am working on a VB .net application which accesses a Java Servlet.
This servlet returns a binary stream that is written onto a PDF and
then
this file is printed. Even though this file is downloaded and printed
on most
environments, it fails to do so on one of the environments with the
following
error:
"Unable to read data from the transport connection."
Also to be noted is the fact that when this servlet is hit from a
different geography, the response is obtained in perfect order and the
file also gets generated. Here's the sample code that does it:

Dim req As HttpWebRequest
Dim res As HttpWebResponse
req = WebRequest.Create("https://**********")
req.Method = "GET"
req.CookieContainer = New CookieContainer
req.CookieContainer.Add(ccContainer.GetCookies(req .RequestUri))
req.ContentType = "text/xml"
' Send the text to the webserver
res = req.GetResponse()
Dim Cook As Cookie
For Each Cook In res.Cookies
Me.ccContainer.Add(Cook)
'MsgBox(Cook.Name)
Next
' Wrap the response stream with a text-based reader
Dim sr As BinaryReader = New BinaryReader(res.GetResponseStream())
' Read the returned text into a textbox
Dim bWriter As New BinaryWriter(File.OpenWrite("temp.pdf"))
bWriter.Write(sr.ReadBytes(99999999))
bWriter.Close()
It seems to me that the problem creeps up when you're trying to access
the response stream to associate with the binary reader. However, I
have not been able to validate this.


There's no point in using a BinaryReader to read raw bytes. Simply read from
the response stream until no more input is available:

using(Stream responseStream = response.GetResponseStream()) {
byte[] buffer = new byte[0x1000];
int bytes;
while((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0) {
// Do something with buffer, e.g. write to FileStream
}
}

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Jan 24 '06 #2
Hi Joerg,
Thanx for such a prompt response.
I found a work around of the problem by specifying the ip andp port
explicitely in my browsers exceptions: 10.xx.xx.xx:7001.
Whereas if we directly access the servlet from a web browser we need not
specify such port settings.
Could you please tell why is it so and what tweak i need to apply in my code
to avoid these additional settings.

Regards,
Sanjeev K Purohit

"Joerg Jooss" wrote:
Hello Sanjeev,
I am working on a VB .net application which accesses a Java Servlet.
This servlet returns a binary stream that is written onto a PDF and
then
this file is printed. Even though this file is downloaded and printed
on most
environments, it fails to do so on one of the environments with the
following
error:
"Unable to read data from the transport connection."
Also to be noted is the fact that when this servlet is hit from a
different geography, the response is obtained in perfect order and the
file also gets generated. Here's the sample code that does it:

Dim req As HttpWebRequest
Dim res As HttpWebResponse
req = WebRequest.Create("https://**********")
req.Method = "GET"
req.CookieContainer = New CookieContainer
req.CookieContainer.Add(ccContainer.GetCookies(req .RequestUri))
req.ContentType = "text/xml"
' Send the text to the webserver
res = req.GetResponse()
Dim Cook As Cookie
For Each Cook In res.Cookies
Me.ccContainer.Add(Cook)
'MsgBox(Cook.Name)
Next
' Wrap the response stream with a text-based reader
Dim sr As BinaryReader = New BinaryReader(res.GetResponseStream())
' Read the returned text into a textbox
Dim bWriter As New BinaryWriter(File.OpenWrite("temp.pdf"))
bWriter.Write(sr.ReadBytes(99999999))
bWriter.Close()
It seems to me that the problem creeps up when you're trying to access
the response stream to associate with the binary reader. However, I
have not been able to validate this.


There's no point in using a BinaryReader to read raw bytes. Simply read from
the response stream until no more input is available:

using(Stream responseStream = response.GetResponseStream()) {
byte[] buffer = new byte[0x1000];
int bytes;
while((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0) {
// Do something with buffer, e.g. write to FileStream
}
}

Cheers,
--
Joerg Jooss
ne********@joergjooss.de

Jan 30 '06 #3
Hello Sanjeev,
Hi Joerg,
Thanx for such a prompt response.
I found a work around of the problem by specifying the ip andp port
explicitely in my browsers exceptions: 10.xx.xx.xx:7001.
Whereas if we directly access the servlet from a web browser we need
not
specify such port settings.
Could you please tell why is it so and what tweak i need to apply in
my code
to avoid these additional settings.
Regards,


What do you mean by "browser exceptions"? IP adresses that are not to be
proxied?

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Jan 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Keith Avery | last post: by
reply views Thread by steve.edison | last post: by
reply views Thread by Brent | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.