调用Https Web服务 - 无法从传输连接读取数据:连接已关闭。 [英] Calling Https web service - Unable to read data from the transport connection: The connection was closed.
问题描述
在从https-web服务中读取文件字节时发现了一个问题。
Found an issues in reading file-bytes from the https-web service.
无论如何,这就是发生的事情:
Anyway, this is what happened:
I我试图将客户端应用程序(.Net 2.0)连接到 https 背后的.Net 1.1 Web服务。我设法获得响应对象,响应返回状态代码OK(200)。当我尝试将responseStream读入字节集合时,就在读取循环结束时,它会抛出此异常:
I'm trying to connect a client-app (.Net 2.0) to a .Net 1.1 Webservice hosted behind a https. I managed to get the response object and the response returned a status code OK(200). When I try to read up the responseStream into bytes collection, right at the end of the reading loop, it throws this exception:
System.IO.IOException:无法从传输连接读取数据:连接已关闭。
System.IO.IOException: Unable to read data from the transport connection: The connection was closed.
请参阅以下代码片段,了解我如何调用webrequest并将响应流读入字节块。
奇怪的是,如果我跟踪代码,在响应流的最后一个字节读取结束时抛出异常(例如,假设Web服务调用返回一个大小为:10,000字节的pdf文件 - 在代码读取10,000之后第n个字节, ReadByte()调用 NOT 返回-1,而是抛出IOExcpetion。
The odd thing is, if I trace through the code, the exception is thrown right at the end of the last byte-read from the response stream (e.g. let say the web service call is returning a pdf file w/ size: 10,000 bytes - right after the code read the 10,000th byte, the ReadByte() call does NOT return -1, instead it throws the IOExcpetion).
我设法通过做一些错误捕捉来解决这个问题,但我确信应该有一种更优雅的方法来解决这个问题。
I managed to get around this problem by doing some error-catching, but I'm sure there should be some kind of a more elegant way of solving this.
其他注释:
- 代码适用于http网络服务
- The code works fine for a http web service
- 我尝试在IE中输入https网址(例如 https://outside.com/Bla.aspx/GetPdf?PdfID= 10 )它显示得很好,浏览器实际上下载了pdf并在浏览器中显示
- I try typing in the https url in the IE (e.g. https://outside.com/Bla.aspx/GetPdf?PdfID=10) and it shows up just fine and the browser actually download the pdf and display it inside the browser
任何帮助都会受到很大的影响
代码段
retResponse =
TryCast (webRequestObj.GetResponse(),HttpWe bResponse)Dim byteColl As Byte()= ReadStreamByte(retResponse.GetResponseStream())
Dim byteColl As Byte() = ReadStreamByte(retResponse.GetResponseStream())
私人 < font size = 2> 共享 功能 ReadStreamByte( ByVal responseStream As Stream ) As < font size = 2> Byte ()
Private Shared Function ReadStreamByte(ByVal responseStream As Stream) As Byte()
Dim readStream As 新 MemoryStream()
Dim readStream As New MemoryStream()
Dim readByte As Int32 = 0
Dim readByte As Int32 = 0
Do
尝试
Try
readByte = responseStream.ReadByte() '如果到达字节的末尾,这行代码将抛出异常(但是从不返回-1?)
readByte = responseStream.ReadByte() 'this line of code will throw exception if the end of the bytes have been reached (but the -1 is never returned ??)
如果 (readByte<> -1) 然后
If (readByte <> -1) Then
readStream.WriteByte(readByte)
readStream.WriteByte(readByte)
End If
End If
Catch ex As IOException
Dim readLen As 整数 = readStream.Length
Dim readLen As Integer = readStream.Length
If (readLen> 0) 然后
If (readLen > 0) Then
退出
Else
Else
投掷
Throw
End If
End Try
Loop readByte<> -1
Loop While readByte <> -1
返回 readStream.ToArray()
Return readStream.ToArray()
End 功能
End Function
推荐答案
实际上,尽管你认为这是是一个网络服务,它不是。您应该在 .NET Framework Networking and Communication中提出问题。
Actually, although you consider this to be a web service, it's not. You should ask the question at .NET Framework Networking and Communication.
这篇关于调用Https Web服务 - 无法从传输连接读取数据:连接已关闭。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!