我正在編寫一個程序,該程序必須通過API樣式調用(POST請求)與基於PHP/Web的後端進行通信。我有現在的代碼是非常緩慢的 - 每個請求需要2-3秒 - 有沒有更快的方法或東西,我可以用它來加快這一當前請求的方法:Visual Basic 2010/.NET |快速POST請求到Web
Function PostData(ByRef Data() As PostData, ByVal URL As String)
Dim Requester As HttpWebRequest = HttpWebRequest.Create(URL)
Requester.Method = "post"
'Requester.Proxy = New System.Net.WebProxy("localhost", 1234)
'Requester.
Dim ASCIIEncoding As Encoding = New ASCIIEncoding
Dim DataString As String = "apirequest=true"
For Each DataItem As PostData In Data
DataString = DataString & "&" & DataItem.Key & "=" & DataItem.Value
Next
DataString = Right(DataString, Len(DataString) - 1)
Dim RawData() As Byte = ASCIIEncoding.GetBytes(DataString)
Requester.Method = "POST"
Requester.ContentType = "application/x-www-form-urlencoded"
Requester.ContentLength = RawData.Length
Requester.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"
Dim NewStream As Stream = Requester.GetRequestStream()
NewStream.Write(RawData, 0, RawData.Length)
NewStream.Close()
Dim ResponseStreamReader As StreamReader = New StreamReader(Requester.GetResponse().GetResponseStream())
Return ResponseStreamReader.ReadToEnd()
End Function
我的互聯網連接非常快,服務器也運行得很快。即使在與其他網站(例如Google)進行測試時,它仍然非常緩慢。 – jSherz 2011-04-01 17:18:57