我正在使用WebClient.UploadData()
在Java服务器上发帖。我该如何延长时间限制? (每次我尝试进行调试时都会超时)
答案 0 :(得分:50)
WebClient没有超时属性,但是可以从WebClient继承以在所使用的内部WebRequest上访问Timeout:
public class WebClientEx : WebClient
{
public int Timeout {get; set;}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
request.Timeout = Timeout;
return request;
}
}
用法:
var myClient = new WebClientEx();
myClient.Timeout = 900000 // Daft timeout period
myClient.UploadData(myUri, myData);
答案 1 :(得分:2)
那么对于那些用VB编码的人来说......
Public Class WebClientExtended
Inherits WebClient
Public Property Timeout() As Integer
Get
Return m_Timeout
End Get
Set(value As Integer)
m_Timeout = value
End Set
End Property
Private m_Timeout As Integer
Protected Overrides Function GetWebRequest(address As Uri) As WebRequest
Dim request = MyBase.GetWebRequest(address)
request.Timeout = Timeout
Return request
End Function
End Class
Function UploadFile(ByVal URL As String, ByVal FilePath As String, ByVal FileName As String)
'Call API to Upload File
Dim myWebClient As New WebClientExtended
myWebClient.Timeout = 10 * 60 * 1000
Dim responseArray As Byte()
Dim responseString As String = ""
Try
responseArray = myWebClient.UploadFile(URL, FilePath + "/" + FileName)
responseString = System.Text.Encoding.ASCII.GetString(responseArray)
Catch ex As Exception
responseString = "Error: " + ex.Message
End Try
Return responseString
End Function