HTTPWebRequest error | Transport connection related

G

Guest

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
 
J

Joerg Jooss

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,
 
G

Guest

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 said:
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,
 
J

Joerg Jooss

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,
 

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

Top