I am using WebClient.UploadData()
to do a post on a Java server. How can I extend the time limit? (It times out every time I am trying to do some debugging)
我使用WebClient.UploadData()在Java服务器上做一个帖子。我怎样才能延长期限?(每次我试着做一些调试时,它就会超时)
2 个解决方案
#1
47
The WebClient doesn't have a timeout property, however it is possible to inherit from the WebClient to give access to Timeout on the internal WebRequest used:
WebClient没有timeout属性,但是可以从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;
}
}
Usage:
用法:
var myClient = new WebClientEx();
myClient.Timeout = 900000 // Daft timeout period
myClient.UploadData(myUri, myData);
#2
2
So for those who code in VB...
所以对于那些用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)
函数UploadFile(ByVal URL作为字符串,ByVal FilePath作为字符串,ByVal文件名作为字符串)
'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
(This is only my second post)
(这只是我的第二个帖子)
#1
47
The WebClient doesn't have a timeout property, however it is possible to inherit from the WebClient to give access to Timeout on the internal WebRequest used:
WebClient没有timeout属性,但是可以从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;
}
}
Usage:
用法:
var myClient = new WebClientEx();
myClient.Timeout = 900000 // Daft timeout period
myClient.UploadData(myUri, myData);
#2
2
So for those who code in VB...
所以对于那些用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)
函数UploadFile(ByVal URL作为字符串,ByVal FilePath作为字符串,ByVal文件名作为字符串)
'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
(This is only my second post)
(这只是我的第二个帖子)