HttpwebRequest和响应花费时间 [英] HttpwebRequest and response taking time
本文介绍了HttpwebRequest和响应花费时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码需要150到200秒的时间才能读取数据(实际上,我的响应数据非常大)
请提供任何替代方法来解决此问题.
below code is taking 150 to 200 second to read data (actualy my response data is very large)
Please give any alternate to resolve this problem.
Protected Function PostXml(ByVal url As String, ByVal xml As String) As String
Dim bytes As Byte() = Encoding.UTF8.GetBytes(xml)
Dim strResult As String = String.Empty
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
request.Method = "POST"
request.ContentLength = bytes.Length
request.ContentType = "text/xml" '; charset=utf-8
request.Timeout = 3600 * 1000
request.ReadWriteTimeout = 3600 * 1000
Try
Using requestStream As Stream = request.GetRequestStream()
requestStream.Write(bytes, 0, bytes.Length)
End Using
Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
If response.StatusCode <> HttpStatusCode.OK Then
Dim message As String = [String].Format("POST failed. Received HTTP {0}", response.StatusCode)
Throw New ApplicationException(message)
Else
Dim reader As StreamReader = Nothing
Dim responseStream As Stream = response.GetResponseStream()
reader = New StreamReader(responseStream, Encoding.Default)
strResult = reader.ReadToEnd() 'Here is taking to much time
response.Close()
responseStream.Close()
reader.Close()
End If
End Using
Catch ex As WebException
strResult = ""
HtlLog.InsertLogDetails(ex)
End Try
Return strResult
End Function
推荐答案
您可能会超出一端或另一端的Internet连接的上载/下载速度限制. 200秒不到4分钟,具体取决于您的数据大小和Internet连接质量,这可能不是不合理的时间.如果是这种情况,那么您需要查看您的实际数据,看看是否可以找到一些自然的断点,从而允许您以更多的请求将其分小块下载.总传输时间可能会增加,但是由于数据开始显示更快,因此可以感知的时间会减少.
You may well be up against the upload / download speed limit of your internet connection at one end or the other. 200 seconds is under 4 minutes, and depending on your data size and internet connection quality, it may not be an unreasonable amount of time. If this is the case, then you need to look at your actual data, and see if you can find some natural break points allowing you to down load it in smaller pieces, with more requests. The total transfer time may go up, but the perceived time will reduce because the data starts to appear quicker.
通过gzip我解决了我的问题
Through gzip I resolve my problem
这篇关于HttpwebRequest和响应花费时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文